
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/ntheory.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:30 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Number Theory &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
    <script src="../_static/jquery.js"></script>
    <script src="../_static/underscore.js"></script>
    <script src="../_static/doctools.js"></script>
    <script src="../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../_static/sympy-notailtext-favicon.ico"/>
    <link href="ntheory.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="Numeric Computation" href="numeric-computation.html" />
    <link rel="prev" title="Matrix Expressions" href="matrices/expressions.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="numeric-computation.html" title="Numeric Computation"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="matrices/expressions.html" title="Matrix Expressions"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" accesskey="U">SymPy Modules Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Number Theory</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="module-sympy.ntheory.generate">
<span id="number-theory"></span><h1>Number Theory<a class="headerlink" href="#module-sympy.ntheory.generate" title="Permalink to this headline">¶</a></h1>
<section id="ntheory-class-reference">
<h2>Ntheory Class Reference<a class="headerlink" href="#ntheory-class-reference" title="Permalink to this headline">¶</a></h2>
<dl class="py class">
<dt class="sig sig-object py" id="sympy.ntheory.generate.Sieve">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.ntheory.generate.</span></span><span class="sig-name descname"><span class="pre">Sieve</span></span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L30-L346"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.Sieve" title="Permalink to this definition">¶</a></dt>
<dd><p>An infinite list of prime numbers, implemented as a dynamically
growing sieve of Eratosthenes. When a lookup is requested involving
an odd number that has not been sieved, the sieve is automatically
extended up to that number.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sieve</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sieve</span><span class="o">.</span><span class="n">_reset</span><span class="p">()</span> <span class="c1"># this line for doctest only</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">25</span> <span class="ow">in</span> <span class="n">sieve</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sieve</span><span class="o">.</span><span class="n">_list</span>
<span class="go">array(&#39;l&#39;, [2, 3, 5, 7, 11, 13, 17, 19, 23])</span>
</pre></div>
</div>
<dl class="py method">
<dt class="sig sig-object py" id="sympy.ntheory.generate.Sieve.extend">
<span class="sig-name descname"><span class="pre">extend</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L81-L116"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.Sieve.extend" title="Permalink to this definition">¶</a></dt>
<dd><p>Grow the sieve to cover all primes &lt;= n (a real number).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sieve</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sieve</span><span class="o">.</span><span class="n">_reset</span><span class="p">()</span> <span class="c1"># this line for doctest only</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sieve</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sieve</span><span class="p">[</span><span class="mi">10</span><span class="p">]</span> <span class="o">==</span> <span class="mi">29</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.ntheory.generate.Sieve.extend_to_no">
<span class="sig-name descname"><span class="pre">extend_to_no</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">i</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L118-L143"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.Sieve.extend_to_no" title="Permalink to this definition">¶</a></dt>
<dd><p>Extend to include the ith prime number.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>i</strong> : integer</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sieve</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sieve</span><span class="o">.</span><span class="n">_reset</span><span class="p">()</span> <span class="c1"># this line for doctest only</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sieve</span><span class="o">.</span><span class="n">extend_to_no</span><span class="p">(</span><span class="mi">9</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sieve</span><span class="o">.</span><span class="n">_list</span>
<span class="go">array(&#39;l&#39;, [2, 3, 5, 7, 11, 13, 17, 19, 23])</span>
</pre></div>
</div>
<p class="rubric">Notes</p>
<p>The list is extended by 50% if it is too short, so it is
likely that it will be longer than requested.</p>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.ntheory.generate.Sieve.mobiusrange">
<span class="sig-name descname"><span class="pre">mobiusrange</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L231-L277"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.Sieve.mobiusrange" title="Permalink to this definition">¶</a></dt>
<dd><p>Generate all mobius numbers for the range [a, b).</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>a</strong> : integer</p>
<blockquote>
<div><p>First number in range</p>
</div></blockquote>
<p><strong>b</strong> : integer</p>
<blockquote>
<div><p>First number outside of range</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sieve</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">([</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">sieve</span><span class="o">.</span><span class="n">mobiusrange</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">18</span><span class="p">)])</span>
<span class="go">[-1, 0, 0, 1, -1, 0, -1, 1, 1, 0, -1]</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.ntheory.generate.Sieve.primerange">
<span class="sig-name descname"><span class="pre">primerange</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L145-L190"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.Sieve.primerange" title="Permalink to this definition">¶</a></dt>
<dd><p>Generate all prime numbers in the range [2, a) or [a, b).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sieve</span><span class="p">,</span> <span class="n">prime</span>
</pre></div>
</div>
<p>All primes less than 19:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">([</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">sieve</span><span class="o">.</span><span class="n">primerange</span><span class="p">(</span><span class="mi">19</span><span class="p">)])</span>
<span class="go">[2, 3, 5, 7, 11, 13, 17]</span>
</pre></div>
</div>
<p>All primes greater than or equal to 7 and less than 19:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">([</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">sieve</span><span class="o">.</span><span class="n">primerange</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">19</span><span class="p">)])</span>
<span class="go">[7, 11, 13, 17]</span>
</pre></div>
</div>
<p>All primes through the 10th prime</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">sieve</span><span class="o">.</span><span class="n">primerange</span><span class="p">(</span><span class="n">prime</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
<span class="go">[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.ntheory.generate.Sieve.search">
<span class="sig-name descname"><span class="pre">search</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L279-L310"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.Sieve.search" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the indices i, j of the primes that bound n.</p>
<p>If n is prime then i == j.</p>
<p>Although n can be an expression, if ceiling cannot convert
it to an integer then an n error will be raised.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sieve</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sieve</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span>
<span class="go">(9, 10)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sieve</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="mi">23</span><span class="p">)</span>
<span class="go">(9, 9)</span>
</pre></div>
</div>
</dd></dl>

<dl class="py method">
<dt class="sig sig-object py" id="sympy.ntheory.generate.Sieve.totientrange">
<span class="sig-name descname"><span class="pre">totientrange</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L192-L229"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.Sieve.totientrange" title="Permalink to this definition">¶</a></dt>
<dd><p>Generate all totient numbers for the range [a, b).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sieve</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">([</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">sieve</span><span class="o">.</span><span class="n">totientrange</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">18</span><span class="p">)])</span>
<span class="go">[6, 4, 6, 4, 10, 4, 12, 6, 8, 8, 16]</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

</section>
<section id="ntheory-functions-reference">
<h2>Ntheory Functions Reference<a class="headerlink" href="#ntheory-functions-reference" title="Permalink to this headline">¶</a></h2>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.generate.prime">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.generate.</span></span><span class="sig-name descname"><span class="pre">prime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">nth</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L352-L424"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.prime" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the nth prime, with the primes indexed as prime(1) = 2,
prime(2) = 3, etc…. The nth prime is approximately n*log(n).</p>
<p>Logarithmic integral of x is a pretty nice approximation for number of
primes &lt;= x, i.e.
li(x) ~ pi(x)
In fact, for the numbers we are concerned about( x&lt;1e11 ),
li(x) - pi(x) &lt; 50000</p>
<p>Also,
li(x) &gt; pi(x) can be safely assumed for the numbers which
can be evaluated by this function.</p>
<p>Here, we find the least integer m such that li(m) &gt; n using binary search.
Now pi(m-1) &lt; li(m-1) &lt;= n,</p>
<p>We find pi(m - 1) using primepi function.</p>
<p>Starting from m, we have to find n - pi(m-1) more primes.</p>
<p>For the inputs this implementation can handle, we will have to test
primality for at max about 10**5 numbers, to get our answer.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">prime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prime</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">29</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prime</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">prime</span><span class="p">(</span><span class="mi">100000</span><span class="p">)</span>
<span class="go">1299709</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#sympy.ntheory.primetest.isprime" title="sympy.ntheory.primetest.isprime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.ntheory.primetest.isprime</span></code></a></dt><dd><p>Test if n is prime</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.primerange" title="sympy.ntheory.generate.primerange"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primerange</span></code></a></dt><dd><p>Generate all primes in a given range</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.primepi" title="sympy.ntheory.generate.primepi"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primepi</span></code></a></dt><dd><p>Return the number of primes less than or equal to n</p>
</dd>
</dl>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r552"><span class="brackets"><a class="fn-backref" href="#id1">R552</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Prime_number_theorem#Table_of_.CF.80.28x.29.2C_x_.2F_log_x.2C_and_li.28x.29">https://en.wikipedia.org/wiki/Prime_number_theorem#Table_of_.CF.80.28x.29.2C_x_.2F_log_x.2C_and_li.28x.29</a></p>
</dd>
<dt class="label" id="r553"><span class="brackets"><a class="fn-backref" href="#id2">R553</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Prime_number_theorem#Approximations_for_the_nth_prime_number">https://en.wikipedia.org/wiki/Prime_number_theorem#Approximations_for_the_nth_prime_number</a></p>
</dd>
<dt class="label" id="r554"><span class="brackets"><a class="fn-backref" href="#id3">R554</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Skewes%27_number">https://en.wikipedia.org/wiki/Skewes%27_number</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.generate.primepi">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.generate.</span></span><span class="sig-name descname"><span class="pre">primepi</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L427-L549"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.primepi" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents the prime counting function pi(n) = the number
of prime numbers less than or equal to n.</p>
<p>Algorithm Description:</p>
<p>In sieve method, we remove all multiples of prime p
except p itself.</p>
<p>Let phi(i,j) be the number of integers 2 &lt;= k &lt;= i
which remain after sieving from primes less than
or equal to j.
Clearly, pi(n) = phi(n, sqrt(n))</p>
<p>If j is not a prime,
phi(i,j) = phi(i, j - 1)</p>
<p>if j is a prime,
We remove all numbers(except j) whose
smallest prime factor is j.</p>
<p>Let x= j*a be such a number, where 2 &lt;= a&lt;= i / j
Now, after sieving from primes &lt;= j - 1,
a must remain
(because x, and hence a has no prime factor &lt;= j - 1)
Clearly, there are phi(i / j, j - 1) such a
which remain on sieving from primes &lt;= j - 1</p>
<p>Now, if a is a prime less than equal to j - 1,
x= j*a has smallest prime factor = a, and
has already been removed(by sieving from a).
So, we don’t need to remove it again.
(Note: there will be pi(j - 1) such x)</p>
<p>Thus, number of x, that will be removed are:
phi(i / j, j - 1) - phi(j - 1, j - 1)
(Note that pi(j - 1) = phi(j - 1, j - 1))</p>
<p>=&gt; phi(i,j) = phi(i, j - 1) - phi(i / j, j - 1) + phi(j - 1, j - 1)</p>
<p>So,following recursion is used and implemented as dp:</p>
<p>phi(a, b) = phi(a, b - 1), if b is not a prime
phi(a, b) = phi(a, b-1)-phi(a / b, b-1) + phi(b-1, b-1), if b is prime</p>
<p>Clearly a is always of the form floor(n / k),
which can take at most 2*sqrt(n) values.
Two arrays arr1,arr2 are maintained
arr1[i] = phi(i, j),
arr2[i] = phi(n // i, j)</p>
<p>Finally the answer is arr2[1]</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">primepi</span><span class="p">,</span> <span class="n">prime</span><span class="p">,</span> <span class="n">prevprime</span><span class="p">,</span> <span class="n">isprime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primepi</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span>
<span class="go">9</span>
</pre></div>
</div>
<p>So there are 9 primes less than or equal to 25. Is 25 prime?</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">isprime</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<p>It isn’t. So the first prime less than 25 must be the
9th prime:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">prevprime</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span> <span class="o">==</span> <span class="n">prime</span><span class="p">(</span><span class="mi">9</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#sympy.ntheory.primetest.isprime" title="sympy.ntheory.primetest.isprime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.ntheory.primetest.isprime</span></code></a></dt><dd><p>Test if n is prime</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.primerange" title="sympy.ntheory.generate.primerange"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primerange</span></code></a></dt><dd><p>Generate all primes in a given range</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.prime" title="sympy.ntheory.generate.prime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">prime</span></code></a></dt><dd><p>Return the nth prime</p>
</dd>
</dl>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.generate.nextprime">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.generate.</span></span><span class="sig-name descname"><span class="pre">nextprime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ith</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L552-L617"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.nextprime" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the ith prime greater than n.</p>
<p>i must be an integer.</p>
<p class="rubric">Notes</p>
<p>Potential primes are located at 6*j +/- 1. This
property is used during searching.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">nextprime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[(</span><span class="n">i</span><span class="p">,</span> <span class="n">nextprime</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">15</span><span class="p">)]</span>
<span class="go">[(10, 11), (11, 13), (12, 13), (13, 17), (14, 17)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nextprime</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">ith</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="c1"># the 2nd prime after 2</span>
<span class="go">5</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#sympy.ntheory.generate.prevprime" title="sympy.ntheory.generate.prevprime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">prevprime</span></code></a></dt><dd><p>Return the largest prime smaller than n</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.primerange" title="sympy.ntheory.generate.primerange"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primerange</span></code></a></dt><dd><p>Generate all primes in a given range</p>
</dd>
</dl>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.generate.prevprime">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.generate.</span></span><span class="sig-name descname"><span class="pre">prevprime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L620-L668"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.prevprime" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the largest prime smaller than n.</p>
<p class="rubric">Notes</p>
<p>Potential primes are located at 6*j +/- 1. This
property is used during searching.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">prevprime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[(</span><span class="n">i</span><span class="p">,</span> <span class="n">prevprime</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">15</span><span class="p">)]</span>
<span class="go">[(10, 7), (11, 7), (12, 11), (13, 11), (14, 13)]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#sympy.ntheory.generate.nextprime" title="sympy.ntheory.generate.nextprime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">nextprime</span></code></a></dt><dd><p>Return the ith prime greater than n</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.primerange" title="sympy.ntheory.generate.primerange"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primerange</span></code></a></dt><dd><p>Generates all primes in a given range</p>
</dd>
</dl>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.generate.primerange">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.generate.</span></span><span class="sig-name descname"><span class="pre">primerange</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L671-L767"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.primerange" title="Permalink to this definition">¶</a></dt>
<dd><p>Generate a list of all prime numbers in the range [2, a),
or [a, b).</p>
<p>If the range exists in the default sieve, the values will
be returned from there; otherwise values will be returned
but will not modify the sieve.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">primerange</span><span class="p">,</span> <span class="n">prime</span>
</pre></div>
</div>
<p>All primes less than 19:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">primerange</span><span class="p">(</span><span class="mi">19</span><span class="p">))</span>
<span class="go">[2, 3, 5, 7, 11, 13, 17]</span>
</pre></div>
</div>
<p>All primes greater than or equal to 7 and less than 19:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">primerange</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">19</span><span class="p">))</span>
<span class="go">[7, 11, 13, 17]</span>
</pre></div>
</div>
<p>All primes through the 10th prime</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">primerange</span><span class="p">(</span><span class="n">prime</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span>
<span class="go">[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]</span>
</pre></div>
</div>
<p>The Sieve method, primerange, is generally faster but it will
occupy more memory as the sieve stores values. The default
instance of Sieve, named sieve, can be used:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sieve</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">sieve</span><span class="o">.</span><span class="n">primerange</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">30</span><span class="p">))</span>
<span class="go">[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]</span>
</pre></div>
</div>
<p class="rubric">Notes</p>
<p>Some famous conjectures about the occurrence of primes in a given
range are [1]:</p>
<ul class="simple">
<li><dl class="simple">
<dt>Twin primes: though often not, the following will give 2 primes</dt><dd><dl class="simple">
<dt>an infinite number of times:</dt><dd><p>primerange(6*n - 1, 6*n + 2)</p>
</dd>
</dl>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt>Legendre’s: the following always yields at least one prime</dt><dd><p>primerange(n**2, (n+1)**2+1)</p>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt>Bertrand’s (proven): there is always a prime in the range</dt><dd><p>primerange(n, 2*n)</p>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt>Brocard’s: there are at least four primes in the range</dt><dd><p>primerange(prime(n)**2, prime(n+1)**2)</p>
</dd>
</dl>
</li>
</ul>
<p>The average gap between primes is log(n) [2]; the gap between
primes can be arbitrarily large since sequences of composite
numbers are arbitrarily large, e.g. the numbers in the sequence
n! + 2, n! + 3 … n! + n are all composite.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#sympy.ntheory.generate.prime" title="sympy.ntheory.generate.prime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">prime</span></code></a></dt><dd><p>Return the nth prime</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.nextprime" title="sympy.ntheory.generate.nextprime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">nextprime</span></code></a></dt><dd><p>Return the ith prime greater than n</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.prevprime" title="sympy.ntheory.generate.prevprime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">prevprime</span></code></a></dt><dd><p>Return the largest prime smaller than n</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.randprime" title="sympy.ntheory.generate.randprime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">randprime</span></code></a></dt><dd><p>Returns a random prime in a given range</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.primorial" title="sympy.ntheory.generate.primorial"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primorial</span></code></a></dt><dd><p>Returns the product of primes based on condition</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.Sieve.primerange" title="sympy.ntheory.generate.Sieve.primerange"><code class="xref py py-obj docutils literal notranslate"><span class="pre">Sieve.primerange</span></code></a></dt><dd><p>return range from already computed primes or extend the sieve to contain the requested range.</p>
</dd>
</dl>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r555"><span class="brackets"><a class="fn-backref" href="#id4">R555</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Prime_number">https://en.wikipedia.org/wiki/Prime_number</a></p>
</dd>
<dt class="label" id="r556"><span class="brackets"><a class="fn-backref" href="#id5">R556</a></span></dt>
<dd><p><a class="reference external" href="http://primes.utm.edu/notes/gaps.html">http://primes.utm.edu/notes/gaps.html</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.generate.randprime">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.generate.</span></span><span class="sig-name descname"><span class="pre">randprime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L770-L805"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.randprime" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a random prime number in the range [a, b).</p>
<p>Bertrand’s postulate assures that
randprime(a, 2*a) will always succeed for a &gt; 1.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">randprime</span><span class="p">,</span> <span class="n">isprime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">randprime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">30</span><span class="p">)</span> 
<span class="go">13</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isprime</span><span class="p">(</span><span class="n">randprime</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">30</span><span class="p">))</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#sympy.ntheory.generate.primerange" title="sympy.ntheory.generate.primerange"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primerange</span></code></a></dt><dd><p>Generate all primes in a given range</p>
</dd>
</dl>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r557"><span class="brackets"><a class="fn-backref" href="#id6">R557</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Bertrand's_postulate">https://en.wikipedia.org/wiki/Bertrand’s_postulate</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.generate.primorial">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.generate.</span></span><span class="sig-name descname"><span class="pre">primorial</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">nth</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L808-L871"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.primorial" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the product of the first n primes (default) or
the primes less than or equal to n (when <code class="docutils literal notranslate"><span class="pre">nth=False</span></code>).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.generate</span> <span class="kn">import</span> <span class="n">primorial</span><span class="p">,</span> <span class="n">primerange</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">factorint</span><span class="p">,</span> <span class="n">Mul</span><span class="p">,</span> <span class="n">primefactors</span><span class="p">,</span> <span class="n">sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primorial</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="c1"># the first 4 primes are 2, 3, 5, 7</span>
<span class="go">210</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primorial</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="n">nth</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span> <span class="c1"># primes &lt;= 4 are 2 and 3</span>
<span class="go">6</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primorial</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primorial</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">nth</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primorial</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">101</span><span class="p">),</span> <span class="n">nth</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">210</span>
</pre></div>
</div>
<p>One can argue that the primes are infinite since if you take
a set of primes and multiply them together (e.g. the primorial) and
then add or subtract 1, the result cannot be divided by any of the
original factors, hence either 1 or more new primes must divide this
product of primes.</p>
<p>In this case, the number itself is a new prime:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factorint</span><span class="p">(</span><span class="n">primorial</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">{211: 1}</span>
</pre></div>
</div>
<p>In this case two new primes are the factors:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factorint</span><span class="p">(</span><span class="n">primorial</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">{11: 1, 19: 1}</span>
</pre></div>
</div>
<p>Here, some primes smaller and larger than the primes multiplied together
are obtained:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">primerange</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">primefactors</span><span class="p">(</span><span class="n">Mul</span><span class="p">(</span><span class="o">*</span><span class="n">p</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">))</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">p</span><span class="p">)))</span>
<span class="go">[2, 5, 31, 149]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#sympy.ntheory.generate.primerange" title="sympy.ntheory.generate.primerange"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primerange</span></code></a></dt><dd><p>Generate all primes in a given range</p>
</dd>
</dl>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.generate.cycle_length">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.generate.</span></span><span class="sig-name descname"><span class="pre">cycle_length</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">f</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">x0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">nmax</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">values</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L874-L960"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.cycle_length" title="Permalink to this definition">¶</a></dt>
<dd><p>For a given iterated sequence, return a generator that gives
the length of the iterated cycle (lambda) and the length of terms
before the cycle begins (mu); if <code class="docutils literal notranslate"><span class="pre">values</span></code> is True then the
terms of the sequence will be returned instead. The sequence is
started with value <code class="docutils literal notranslate"><span class="pre">x0</span></code>.</p>
<p>Note: more than the first lambda + mu terms may be returned and this
is the cost of cycle detection with Brent’s method; there are, however,
generally less terms calculated than would have been calculated if the
proper ending point were determined, e.g. by using Floyd’s method.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.generate</span> <span class="kn">import</span> <span class="n">cycle_length</span>
</pre></div>
</div>
<p>This will yield successive values of i &lt;– func(i):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">iter</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">while</span> <span class="mi">1</span><span class="p">:</span>
<span class="gp">... </span>        <span class="n">ii</span> <span class="o">=</span> <span class="n">func</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="gp">... </span>        <span class="k">yield</span> <span class="n">ii</span>
<span class="gp">... </span>        <span class="n">i</span> <span class="o">=</span> <span class="n">ii</span>
<span class="gp">...</span>
</pre></div>
</div>
<p>A function is defined:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">func</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">i</span><span class="p">:</span> <span class="p">(</span><span class="n">i</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="mi">51</span>
</pre></div>
</div>
<p>and given a seed of 4 and the mu and lambda terms calculated:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">cycle_length</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span>
<span class="go">(6, 2)</span>
</pre></div>
</div>
<p>We can see what is meant by looking at the output:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">cycle_length</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">values</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">ni</span> <span class="k">for</span> <span class="n">ni</span> <span class="ow">in</span> <span class="n">n</span><span class="p">)</span>
<span class="go">[17, 35, 2, 5, 26, 14, 44, 50, 2, 5, 26, 14]</span>
</pre></div>
</div>
<p>There are 6 repeating values after the first 2.</p>
<p>If a sequence is suspected of being longer than you might wish, <code class="docutils literal notranslate"><span class="pre">nmax</span></code>
can be used to exit early (and mu will be returned as None):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">cycle_length</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">nmax</span> <span class="o">=</span> <span class="mi">4</span><span class="p">))</span>
<span class="go">(4, None)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">ni</span> <span class="k">for</span> <span class="n">ni</span> <span class="ow">in</span> <span class="n">cycle_length</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="n">nmax</span> <span class="o">=</span> <span class="mi">4</span><span class="p">,</span> <span class="n">values</span><span class="o">=</span><span class="kc">True</span><span class="p">)]</span>
<span class="go">[17, 35, 2, 5]</span>
</pre></div>
</div>
<dl class="simple">
<dt>Code modified from:</dt><dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Cycle_detection">https://en.wikipedia.org/wiki/Cycle_detection</a>.</p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.generate.composite">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.generate.</span></span><span class="sig-name descname"><span class="pre">composite</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">nth</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L963-L1026"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.composite" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the nth composite number, with the composite numbers indexed as
composite(1) = 4, composite(2) = 6, etc….</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">composite</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">composite</span><span class="p">(</span><span class="mi">36</span><span class="p">)</span>
<span class="go">52</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">composite</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">composite</span><span class="p">(</span><span class="mi">17737</span><span class="p">)</span>
<span class="go">20000</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#sympy.ntheory.primetest.isprime" title="sympy.ntheory.primetest.isprime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.ntheory.primetest.isprime</span></code></a></dt><dd><p>Test if n is prime</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.primerange" title="sympy.ntheory.generate.primerange"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primerange</span></code></a></dt><dd><p>Generate all primes in a given range</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.primepi" title="sympy.ntheory.generate.primepi"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primepi</span></code></a></dt><dd><p>Return the number of primes less than or equal to n</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.prime" title="sympy.ntheory.generate.prime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">prime</span></code></a></dt><dd><p>Return the nth prime</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.compositepi" title="sympy.ntheory.generate.compositepi"><code class="xref py py-obj docutils literal notranslate"><span class="pre">compositepi</span></code></a></dt><dd><p>Return the number of positive composite numbers less than or equal to n</p>
</dd>
</dl>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.generate.compositepi">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.generate.</span></span><span class="sig-name descname"><span class="pre">compositepi</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/generate.py#L1029-L1054"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.generate.compositepi" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the number of positive composite numbers less than or equal to n.
The first positive composite is 4, i.e. compositepi(4) = 1.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">compositepi</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compositepi</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span>
<span class="go">15</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">compositepi</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span>
<span class="go">831</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#sympy.ntheory.primetest.isprime" title="sympy.ntheory.primetest.isprime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.ntheory.primetest.isprime</span></code></a></dt><dd><p>Test if n is prime</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.primerange" title="sympy.ntheory.generate.primerange"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primerange</span></code></a></dt><dd><p>Generate all primes in a given range</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.prime" title="sympy.ntheory.generate.prime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">prime</span></code></a></dt><dd><p>Return the nth prime</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.primepi" title="sympy.ntheory.generate.primepi"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primepi</span></code></a></dt><dd><p>Return the number of primes less than or equal to n</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.composite" title="sympy.ntheory.generate.composite"><code class="xref py py-obj docutils literal notranslate"><span class="pre">composite</span></code></a></dt><dd><p>Return the nth composite number</p>
</dd>
</dl>
</div>
</dd></dl>

<span class="target" id="module-sympy.ntheory.factor_"></span><dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.smoothness">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">smoothness</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L65-L92"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.smoothness" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the B-smooth and B-power smooth values of n.</p>
<p>The smoothness of n is the largest prime factor of n; the power-
smoothness is the largest divisor raised to its multiplicity.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">smoothness</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">smoothness</span><span class="p">(</span><span class="mi">2</span><span class="o">**</span><span class="mi">7</span><span class="o">*</span><span class="mi">3</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(3, 128)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">smoothness</span><span class="p">(</span><span class="mi">2</span><span class="o">**</span><span class="mi">4</span><span class="o">*</span><span class="mi">13</span><span class="p">)</span>
<span class="go">(13, 16)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">smoothness</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">(2, 2)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.smoothness_p" title="sympy.ntheory.factor_.smoothness_p"><code class="xref py py-obj docutils literal notranslate"><span class="pre">smoothness_p</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.smoothness_p">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">smoothness_p</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">m</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">-</span> <span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">power</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">visual</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L95-L195"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.smoothness_p" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of [m, (p, (M, sm(p + m), psm(p + m)))…]
where:</p>
<ol class="arabic simple">
<li><p>p**M is the base-p divisor of n</p></li>
<li><p>sm(p + m) is the smoothness of p + m (m = -1 by default)</p></li>
<li><p>psm(p + m) is the power smoothness of p + m</p></li>
</ol>
<p>The list is sorted according to smoothness (default) or by power smoothness
if power=1.</p>
<p>The smoothness of the numbers to the left (m = -1) or right (m = 1) of a
factor govern the results that are obtained from the p +/- 1 type factoring
methods.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">smoothness_p</span><span class="p">,</span> <span class="n">factorint</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">smoothness_p</span><span class="p">(</span><span class="mi">10431</span><span class="p">,</span> <span class="n">m</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="go">(1, [(3, (2, 2, 4)), (19, (1, 5, 5)), (61, (1, 31, 31))])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">smoothness_p</span><span class="p">(</span><span class="mi">10431</span><span class="p">)</span>
<span class="go">(-1, [(3, (2, 2, 2)), (19, (1, 3, 9)), (61, (1, 5, 5))])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">smoothness_p</span><span class="p">(</span><span class="mi">10431</span><span class="p">,</span> <span class="n">power</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="go">(-1, [(3, (2, 2, 2)), (61, (1, 5, 5)), (19, (1, 3, 9))])</span>
</pre></div>
</div>
<p>If visual=True then an annotated string will be returned:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">smoothness_p</span><span class="p">(</span><span class="mi">21477639576571</span><span class="p">,</span> <span class="n">visual</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
<span class="go">p**i=4410317**1 has p-1 B=1787, B-pow=1787</span>
<span class="go">p**i=4869863**1 has p-1 B=2434931, B-pow=2434931</span>
</pre></div>
</div>
<p>This string can also be generated directly from a factorization dictionary
and vice versa:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factorint</span><span class="p">(</span><span class="mi">17</span><span class="o">*</span><span class="mi">9</span><span class="p">)</span>
<span class="go">{3: 2, 17: 1}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">smoothness_p</span><span class="p">(</span><span class="n">_</span><span class="p">)</span>
<span class="go">&#39;p**i=3**2 has p-1 B=2, B-pow=2\np**i=17**1 has p-1 B=2, B-pow=16&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">smoothness_p</span><span class="p">(</span><span class="n">_</span><span class="p">)</span>
<span class="go">{3: 2, 17: 1}</span>
</pre></div>
</div>
<p>The table of the output logic is:</p>
<blockquote>
<div><table class="docutils align-default">
<colgroup>
<col style="width: 23%" />
<col style="width: 23%" />
<col style="width: 27%" />
<col style="width: 27%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><div class="line-block">
<div class="line"><br /></div>
</div>
</th>
<th class="head" colspan="3"><p>Visual</p></th>
</tr>
<tr class="row-even"><th class="head"><p>Input</p></th>
<th class="head"><p>True</p></th>
<th class="head"><p>False</p></th>
<th class="head"><p>other</p></th>
</tr>
</thead>
<tbody>
<tr class="row-odd"><td><p>dict</p></td>
<td><p>str</p></td>
<td><p>tuple</p></td>
<td><p>str</p></td>
</tr>
<tr class="row-even"><td><p>str</p></td>
<td><p>str</p></td>
<td><p>tuple</p></td>
<td><p>dict</p></td>
</tr>
<tr class="row-odd"><td><p>tuple</p></td>
<td><p>str</p></td>
<td><p>tuple</p></td>
<td><p>str</p></td>
</tr>
<tr class="row-even"><td><p>n</p></td>
<td><p>str</p></td>
<td><p>tuple</p></td>
<td><p>tuple</p></td>
</tr>
<tr class="row-odd"><td><p>mul</p></td>
<td><p>str</p></td>
<td><p>tuple</p></td>
<td><p>tuple</p></td>
</tr>
</tbody>
</table>
</div></blockquote>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.smoothness" title="sympy.ntheory.factor_.smoothness"><code class="xref py py-obj docutils literal notranslate"><span class="pre">smoothness</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.trailing">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">trailing</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L198-L244"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.trailing" title="Permalink to this definition">¶</a></dt>
<dd><p>Count the number of trailing zero digits in the binary
representation of n, i.e. determine the largest power of 2
that divides n.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">trailing</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">trailing</span><span class="p">(</span><span class="mi">128</span><span class="p">)</span>
<span class="go">7</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">trailing</span><span class="p">(</span><span class="mi">63</span><span class="p">)</span>
<span class="go">0</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.multiplicity">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">multiplicity</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">p</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L247-L333"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.multiplicity" title="Permalink to this definition">¶</a></dt>
<dd><p>Find the greatest integer m such that p**m divides n.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">multiplicity</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.core.numbers</span> <span class="kn">import</span> <span class="n">Rational</span> <span class="k">as</span> <span class="n">R</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">multiplicity</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">8</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">125</span><span class="p">,</span> <span class="mi">250</span><span class="p">]]</span>
<span class="go">[0, 1, 2, 3, 3]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">multiplicity</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="n">R</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">9</span><span class="p">))</span>
<span class="go">-2</span>
</pre></div>
</div>
<p>Note: when checking for the multiplicity of a number in a
large factorial it is most efficient to send it as an unevaluated
factorial or to call <code class="docutils literal notranslate"><span class="pre">multiplicity_in_factorial</span></code> directly:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">multiplicity_in_factorial</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">factorial</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">p</span> <span class="o">=</span> <span class="n">factorial</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">2</span><span class="o">**</span><span class="mi">100</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nfac</span> <span class="o">=</span> <span class="n">factorial</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">evaluate</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">multiplicity</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">nfac</span><span class="p">)</span>
<span class="go">52818775009509558395695966887</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">_</span> <span class="o">==</span> <span class="n">multiplicity_in_factorial</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.perfect_power">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">perfect_power</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">candidates</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">big</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">factor</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L401-L536"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.perfect_power" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <code class="docutils literal notranslate"><span class="pre">(b,</span> <span class="pre">e)</span></code> such that <code class="docutils literal notranslate"><span class="pre">n</span></code> == <code class="docutils literal notranslate"><span class="pre">b**e</span></code> if <code class="docutils literal notranslate"><span class="pre">n</span></code> is a
perfect power with <code class="docutils literal notranslate"><span class="pre">e</span> <span class="pre">&gt;</span> <span class="pre">1</span></code>, else <code class="docutils literal notranslate"><span class="pre">False</span></code>. A ValueError is
raised if <code class="docutils literal notranslate"><span class="pre">n</span></code> is not an integer or is not positive.</p>
<p>By default, the base is recursively decomposed and the exponents
collected so the largest possible <code class="docutils literal notranslate"><span class="pre">e</span></code> is sought. If <code class="docutils literal notranslate"><span class="pre">big=False</span></code>
then the smallest possible <code class="docutils literal notranslate"><span class="pre">e</span></code> (thus prime) will be chosen.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">factor=True</span></code> then simultaneous factorization of <code class="docutils literal notranslate"><span class="pre">n</span></code> is
attempted since finding a factor indicates the only possible root
for <code class="docutils literal notranslate"><span class="pre">n</span></code>. This is True by default since only a few small factors will
be tested in the course of searching for the perfect power.</p>
<p>The use of <code class="docutils literal notranslate"><span class="pre">candidates</span></code> is primarily for internal use; if provided,
False will be returned if <code class="docutils literal notranslate"><span class="pre">n</span></code> cannot be written as a power with one
of the candidates as an exponent and factoring (beyond testing for
a factor of 2) will not be attempted.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">perfect_power</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">perfect_power</span><span class="p">(</span><span class="mi">16</span><span class="p">)</span>
<span class="go">(2, 4)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">perfect_power</span><span class="p">(</span><span class="mi">16</span><span class="p">,</span> <span class="n">big</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">(4, 2)</span>
</pre></div>
</div>
<p class="rubric">Notes</p>
<p>To know whether an integer is a perfect power of 2 use</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">is2pow</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">n</span><span class="p">:</span> <span class="nb">bool</span><span class="p">(</span><span class="n">n</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">n</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[(</span><span class="n">i</span><span class="p">,</span> <span class="n">is2pow</span><span class="p">(</span><span class="n">i</span><span class="p">))</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)]</span>
<span class="go">[(0, False), (1, True), (2, True), (3, False), (4, True)]</span>
</pre></div>
</div>
<p>It is not necessary to provide <code class="docutils literal notranslate"><span class="pre">candidates</span></code>. When provided
it will be assumed that they are ints. The first one that is
larger than the computed maximum possible exponent will signal
failure for the routine.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">perfect_power</span><span class="p">(</span><span class="mi">3</span><span class="o">**</span><span class="mi">8</span><span class="p">,</span> <span class="p">[</span><span class="mi">9</span><span class="p">])</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">perfect_power</span><span class="p">(</span><span class="mi">3</span><span class="o">**</span><span class="mi">8</span><span class="p">,</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">8</span><span class="p">])</span>
<span class="go">(3, 8)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">perfect_power</span><span class="p">(</span><span class="mi">3</span><span class="o">**</span><span class="mi">8</span><span class="p">,</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">8</span><span class="p">],</span> <span class="n">big</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">(9, 4)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="core.html#sympy.core.power.integer_nthroot" title="sympy.core.power.integer_nthroot"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.core.power.integer_nthroot</span></code></a>, <a class="reference internal" href="#sympy.ntheory.primetest.is_square" title="sympy.ntheory.primetest.is_square"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.ntheory.primetest.is_square</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.pollard_rho">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">pollard_rho</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">s</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">2</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">a</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">retries</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">5</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">seed</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1234</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_steps</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">F</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L539-L651"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.pollard_rho" title="Permalink to this definition">¶</a></dt>
<dd><p>Use Pollard’s rho method to try to extract a nontrivial factor
of <code class="docutils literal notranslate"><span class="pre">n</span></code>. The returned factor may be a composite number. If no
factor is found, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.</p>
<p>The algorithm generates pseudo-random values of x with a generator
function, replacing x with F(x). If F is not supplied then the
function x**2 + <code class="docutils literal notranslate"><span class="pre">a</span></code> is used. The first value supplied to F(x) is <code class="docutils literal notranslate"><span class="pre">s</span></code>.
Upon failure (if <code class="docutils literal notranslate"><span class="pre">retries</span></code> is &gt; 0) a new <code class="docutils literal notranslate"><span class="pre">a</span></code> and <code class="docutils literal notranslate"><span class="pre">s</span></code> will be
supplied; the <code class="docutils literal notranslate"><span class="pre">a</span></code> will be ignored if F was supplied.</p>
<p>The sequence of numbers generated by such functions generally have a
a lead-up to some number and then loop around back to that number and
begin to repeat the sequence, e.g. 1, 2, 3, 4, 5, 3, 4, 5 – this leader
and loop look a bit like the Greek letter rho, and thus the name, ‘rho’.</p>
<p>For a given function, very different leader-loop values can be obtained
so it is a good idea to allow for retries:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.generate</span> <span class="kn">import</span> <span class="n">cycle_length</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mi">16843009</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">F</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:(</span><span class="mi">2048</span><span class="o">*</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="o">+</span> <span class="mi">32767</span><span class="p">)</span> <span class="o">%</span> <span class="n">n</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">):</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;loop length = </span><span class="si">%4i</span><span class="s1">; leader length = </span><span class="si">%3i</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">next</span><span class="p">(</span><span class="n">cycle_length</span><span class="p">(</span><span class="n">F</span><span class="p">,</span> <span class="n">s</span><span class="p">)))</span>
<span class="gp">...</span>
<span class="go">loop length = 2489; leader length =  42</span>
<span class="go">loop length =   78; leader length = 120</span>
<span class="go">loop length = 1482; leader length =  99</span>
<span class="go">loop length = 1482; leader length = 285</span>
<span class="go">loop length = 1482; leader length = 100</span>
</pre></div>
</div>
<p>Here is an explicit example where there is a two element leadup to
a sequence of 3 numbers (11, 14, 4) that then repeat:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span><span class="o">=</span><span class="mi">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">9</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span><span class="o">=</span><span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="o">+</span><span class="mi">12</span><span class="p">)</span><span class="o">%</span><span class="mi">17</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="gp">...</span>
<span class="go">16</span>
<span class="go">13</span>
<span class="go">11</span>
<span class="go">14</span>
<span class="go">4</span>
<span class="go">11</span>
<span class="go">14</span>
<span class="go">4</span>
<span class="go">11</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">cycle_length</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="o">+</span><span class="mi">12</span><span class="p">)</span><span class="o">%</span><span class="mi">17</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="go">(3, 2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">cycle_length</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="p">(</span><span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="o">+</span><span class="mi">12</span><span class="p">)</span><span class="o">%</span><span class="mi">17</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="n">values</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">[16, 13, 11, 14, 4]</span>
</pre></div>
</div>
<p>Instead of checking the differences of all generated values for a gcd
with n, only the kth and 2*kth numbers are checked, e.g. 1st and 2nd,
2nd and 4th, 3rd and 6th until it has been detected that the loop has been
traversed. Loops may be many thousands of steps long before rho finds a
factor or reports failure. If <code class="docutils literal notranslate"><span class="pre">max_steps</span></code> is specified, the iteration
is cancelled with a failure after the specified number of steps.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">pollard_rho</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="o">=</span><span class="mi">16843009</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">F</span><span class="o">=</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:(</span><span class="mi">2048</span><span class="o">*</span><span class="nb">pow</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="n">n</span><span class="p">)</span> <span class="o">+</span> <span class="mi">32767</span><span class="p">)</span> <span class="o">%</span> <span class="n">n</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pollard_rho</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">F</span><span class="o">=</span><span class="n">F</span><span class="p">)</span>
<span class="go">257</span>
</pre></div>
</div>
<p>Use the default setting with a bad value of <code class="docutils literal notranslate"><span class="pre">a</span></code> and no retries:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pollard_rho</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="n">n</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="n">retries</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p>If retries is &gt; 0 then perhaps the problem will correct itself when
new values are generated for a:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pollard_rho</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="n">n</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="n">retries</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="go">257</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r558"><span class="brackets"><a class="fn-backref" href="#id7">R558</a></span></dt>
<dd><p>Richard Crandall &amp; Carl Pomerance (2005), “Prime Numbers:
A Computational Perspective”, Springer, 2nd edition, 229-231</p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.pollard_pm1">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">pollard_pm1</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">B</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">10</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">a</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">2</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">retries</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">seed</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1234</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L654-L806"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.pollard_pm1" title="Permalink to this definition">¶</a></dt>
<dd><p>Use Pollard’s p-1 method to try to extract a nontrivial factor
of <code class="docutils literal notranslate"><span class="pre">n</span></code>. Either a divisor (perhaps composite) or <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.</p>
<p>The value of <code class="docutils literal notranslate"><span class="pre">a</span></code> is the base that is used in the test gcd(a**M - 1, n).
The default is 2.  If <code class="docutils literal notranslate"><span class="pre">retries</span></code> &gt; 0 then if no factor is found after the
first attempt, a new <code class="docutils literal notranslate"><span class="pre">a</span></code> will be generated randomly (using the <code class="docutils literal notranslate"><span class="pre">seed</span></code>)
and the process repeated.</p>
<p>Note: the value of M is lcm(1..B) = reduce(ilcm, range(2, B + 1)).</p>
<p>A search is made for factors next to even numbers having a power smoothness
less than <code class="docutils literal notranslate"><span class="pre">B</span></code>. Choosing a larger B increases the likelihood of finding a
larger factor but takes longer. Whether a factor of n is found or not
depends on <code class="docutils literal notranslate"><span class="pre">a</span></code> and the power smoothness of the even number just less than
the factor p (hence the name p - 1).</p>
<p>Although some discussion of what constitutes a good <code class="docutils literal notranslate"><span class="pre">a</span></code> some
descriptions are hard to interpret. At the modular.math site referenced
below it is stated that if gcd(a**M - 1, n) = N then a**M % q**r is 1
for every prime power divisor of N. But consider the following:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">smoothness_p</span><span class="p">,</span> <span class="n">pollard_pm1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span><span class="o">=</span><span class="mi">257</span><span class="o">*</span><span class="mi">1009</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">smoothness_p</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="go">(-1, [(257, (1, 2, 256)), (1009, (1, 7, 16))])</span>
</pre></div>
</div>
<p>So we should (and can) find a root with B=16:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pollard_pm1</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">B</span><span class="o">=</span><span class="mi">16</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="go">1009</span>
</pre></div>
</div>
<p>If we attempt to increase B to 256 we find that it doesn’t work:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pollard_pm1</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">B</span><span class="o">=</span><span class="mi">256</span><span class="p">)</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>But if the value of <code class="docutils literal notranslate"><span class="pre">a</span></code> is changed we find that only multiples of
257 work, e.g.:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pollard_pm1</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">B</span><span class="o">=</span><span class="mi">256</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="mi">257</span><span class="p">)</span>
<span class="go">1009</span>
</pre></div>
</div>
<p>Checking different <code class="docutils literal notranslate"><span class="pre">a</span></code> values shows that all the ones that didn’t
work had a gcd value not equal to <code class="docutils literal notranslate"><span class="pre">n</span></code> but equal to one of the
factors:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.core.numbers</span> <span class="kn">import</span> <span class="n">ilcm</span><span class="p">,</span> <span class="n">igcd</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">factorint</span><span class="p">,</span> <span class="n">Pow</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">M</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">256</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">M</span> <span class="o">=</span> <span class="n">ilcm</span><span class="p">(</span><span class="n">M</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">set</span><span class="p">([</span><span class="n">igcd</span><span class="p">(</span><span class="nb">pow</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">256</span><span class="p">)</span> <span class="k">if</span>
<span class="gp">... </span>     <span class="n">igcd</span><span class="p">(</span><span class="nb">pow</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span> <span class="o">!=</span> <span class="n">n</span><span class="p">])</span>
<span class="go">{1009}</span>
</pre></div>
</div>
<p>But does aM % d for every divisor of n give 1?</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">aM</span> <span class="o">=</span> <span class="nb">pow</span><span class="p">(</span><span class="mi">255</span><span class="p">,</span> <span class="n">M</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[(</span><span class="n">d</span><span class="p">,</span> <span class="n">aM</span><span class="o">%</span><span class="n">Pow</span><span class="p">(</span><span class="o">*</span><span class="n">d</span><span class="o">.</span><span class="n">args</span><span class="p">))</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">factorint</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">visual</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span><span class="o">.</span><span class="n">args</span><span class="p">]</span>
<span class="go">[(257**1, 1), (1009**1, 1)]</span>
</pre></div>
</div>
<p>No, only one of them. So perhaps the principle is that a root will
be found for a given value of B provided that:</p>
<ol class="arabic simple">
<li><p>the power smoothness of the p - 1 value next to the root
does not exceed B</p></li>
<li><p>a**M % p != 1 for any of the divisors of n.</p></li>
</ol>
<p>By trying more than one <code class="docutils literal notranslate"><span class="pre">a</span></code> it is possible that one of them
will yield a factor.</p>
<p class="rubric">Examples</p>
<p>With the default smoothness bound, this number can’t be cracked:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">pollard_pm1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pollard_pm1</span><span class="p">(</span><span class="mi">21477639576571</span><span class="p">)</span>
</pre></div>
</div>
<p>Increasing the smoothness bound helps:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pollard_pm1</span><span class="p">(</span><span class="mi">21477639576571</span><span class="p">,</span> <span class="n">B</span><span class="o">=</span><span class="mi">2000</span><span class="p">)</span>
<span class="go">4410317</span>
</pre></div>
</div>
<p>Looking at the smoothness of the factors of this number we find:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">smoothness_p</span><span class="p">,</span> <span class="n">factorint</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">smoothness_p</span><span class="p">(</span><span class="mi">21477639576571</span><span class="p">,</span> <span class="n">visual</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
<span class="go">p**i=4410317**1 has p-1 B=1787, B-pow=1787</span>
<span class="go">p**i=4869863**1 has p-1 B=2434931, B-pow=2434931</span>
</pre></div>
</div>
<p>The B and B-pow are the same for the p - 1 factorizations of the divisors
because those factorizations had a very large prime factor:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factorint</span><span class="p">(</span><span class="mi">4410317</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">{2: 2, 617: 1, 1787: 1}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">factorint</span><span class="p">(</span><span class="mi">4869863</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>
<span class="go">{2: 1, 2434931: 1}</span>
</pre></div>
</div>
<p>Note that until B reaches the B-pow value of 1787, the number is not cracked;</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pollard_pm1</span><span class="p">(</span><span class="mi">21477639576571</span><span class="p">,</span> <span class="n">B</span><span class="o">=</span><span class="mi">1786</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pollard_pm1</span><span class="p">(</span><span class="mi">21477639576571</span><span class="p">,</span> <span class="n">B</span><span class="o">=</span><span class="mi">1787</span><span class="p">)</span>
<span class="go">4410317</span>
</pre></div>
</div>
<p>The B value has to do with the factors of the number next to the divisor,
not the divisors themselves. A worst case scenario is that the number next
to the factor p has a large prime divisisor or is a perfect power. If these
conditions apply then the power-smoothness will be about p/2 or p. The more
realistic is that there will be a large prime factor next to p requiring
a B value on the order of p/2. Although primes may have been searched for
up to this level, the p/2 is a factor of p - 1, something that we don’t
know. The modular.math reference below states that 15% of numbers in the
range of 10**15 to 15**15 + 10**4 are 10**6 power smooth so a B of 10**6
will fail 85% of the time in that range. From 10**8 to 10**8 + 10**3 the
percentages are nearly reversed…but in that range the simple trial
division is quite fast.</p>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r559"><span class="brackets"><a class="fn-backref" href="#id8">R559</a></span></dt>
<dd><p>Richard Crandall &amp; Carl Pomerance (2005), “Prime Numbers:
A Computational Perspective”, Springer, 2nd edition, 236-238</p>
</dd>
<dt class="label" id="r560"><span class="brackets"><a class="fn-backref" href="#id9">R560</a></span></dt>
<dd><p><a class="reference external" href="http://modular.math.washington.edu/edu/2007/spring/ent/ent-html/node81.html">http://modular.math.washington.edu/edu/2007/spring/ent/ent-html/node81.html</a></p>
</dd>
<dt class="label" id="r561"><span class="brackets"><a class="fn-backref" href="#id10">R561</a></span></dt>
<dd><p><a class="reference external" href="https://www.cs.toronto.edu/~yuvalf/Factorization.pdf">https://www.cs.toronto.edu/~yuvalf/Factorization.pdf</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.factorint">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">factorint</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">limit</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_trial</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_rho</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_pm1</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_ecm</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">visual</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">multiple</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L976-L1421"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.factorint" title="Permalink to this definition">¶</a></dt>
<dd><p>Given a positive integer <code class="docutils literal notranslate"><span class="pre">n</span></code>, <code class="docutils literal notranslate"><span class="pre">factorint(n)</span></code> returns a dict containing
the prime factors of <code class="docutils literal notranslate"><span class="pre">n</span></code> as keys and their respective multiplicities
as values. For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">factorint</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">factorint</span><span class="p">(</span><span class="mi">2000</span><span class="p">)</span>    <span class="c1"># 2000 = (2**4) * (5**3)</span>
<span class="go">{2: 4, 5: 3}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">factorint</span><span class="p">(</span><span class="mi">65537</span><span class="p">)</span>   <span class="c1"># This number is prime</span>
<span class="go">{65537: 1}</span>
</pre></div>
</div>
<p>For input less than 2, factorint behaves as follows:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">factorint(1)</span></code> returns the empty factorization, <code class="docutils literal notranslate"><span class="pre">{}</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">factorint(0)</span></code> returns <code class="docutils literal notranslate"><span class="pre">{0:1}</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">factorint(-n)</span></code> adds <code class="docutils literal notranslate"><span class="pre">-1:1</span></code> to the factors and then factors <code class="docutils literal notranslate"><span class="pre">n</span></code></p></li>
</ul>
</div></blockquote>
<p>Partial Factorization:</p>
<p>If <code class="docutils literal notranslate"><span class="pre">limit</span></code> (&gt; 3) is specified, the search is stopped after performing
trial division up to (and including) the limit (or taking a
corresponding number of rho/p-1 steps). This is useful if one has
a large number and only is interested in finding small factors (if
any). Note that setting a limit does not prevent larger factors
from being found early; it simply means that the largest factor may
be composite. Since checking for perfect power is relatively cheap, it is
done regardless of the limit setting.</p>
<p>This number, for example, has two small factors and a huge
semi-prime factor that cannot be reduced easily:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">isprime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="mi">1407633717262338957430697921446883</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">=</span> <span class="n">factorint</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">10000</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span> <span class="o">==</span> <span class="p">{</span><span class="mi">991</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="mi">202916782076162456022877024859</span><span class="p">):</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">7</span><span class="p">:</span> <span class="mi">1</span><span class="p">}</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isprime</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">f</span><span class="p">))</span>
<span class="go">False</span>
</pre></div>
</div>
<p>This number has a small factor and a residual perfect power whose
base is greater than the limit:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factorint</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="mi">101</span><span class="o">**</span><span class="mi">7</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">5</span><span class="p">)</span>
<span class="go">{3: 1, 101: 7}</span>
</pre></div>
</div>
<p>List of Factors:</p>
<p>If <code class="docutils literal notranslate"><span class="pre">multiple</span></code> is set to <code class="docutils literal notranslate"><span class="pre">True</span></code> then a list containing the
prime factors including multiplicities is returned.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factorint</span><span class="p">(</span><span class="mi">24</span><span class="p">,</span> <span class="n">multiple</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">[2, 2, 2, 3]</span>
</pre></div>
</div>
<p>Visual Factorization:</p>
<p>If <code class="docutils literal notranslate"><span class="pre">visual</span></code> is set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, then it will return a visual
factorization of the integer.  For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">pprint</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">factorint</span><span class="p">(</span><span class="mi">4200</span><span class="p">,</span> <span class="n">visual</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go"> 3  1  2  1</span>
<span class="go">2 *3 *5 *7</span>
</pre></div>
</div>
<p>Note that this is achieved by using the evaluate=False flag in Mul
and Pow. If you do other manipulations with an expression where
evaluate=False, it may evaluate.  Therefore, you should use the
visual option only for visualization, and use the normal dictionary
returned by visual=False if you want to perform operations on the
factors.</p>
<p>You can easily switch between the two forms by sending them back to
factorint:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Mul</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">regular</span> <span class="o">=</span> <span class="n">factorint</span><span class="p">(</span><span class="mi">1764</span><span class="p">);</span> <span class="n">regular</span>
<span class="go">{2: 2, 3: 2, 7: 2}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pprint</span><span class="p">(</span><span class="n">factorint</span><span class="p">(</span><span class="n">regular</span><span class="p">))</span>
<span class="go"> 2  2  2</span>
<span class="go">2 *3 *7</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">visual</span> <span class="o">=</span> <span class="n">factorint</span><span class="p">(</span><span class="mi">1764</span><span class="p">,</span> <span class="n">visual</span><span class="o">=</span><span class="kc">True</span><span class="p">);</span> <span class="n">pprint</span><span class="p">(</span><span class="n">visual</span><span class="p">)</span>
<span class="go"> 2  2  2</span>
<span class="go">2 *3 *7</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">factorint</span><span class="p">(</span><span class="n">visual</span><span class="p">))</span>
<span class="go">{2: 2, 3: 2, 7: 2}</span>
</pre></div>
</div>
<p>If you want to send a number to be factored in a partially factored form
you can do so with a dictionary or unevaluated expression:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factorint</span><span class="p">(</span><span class="n">factorint</span><span class="p">({</span><span class="mi">4</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">12</span><span class="p">:</span> <span class="mi">3</span><span class="p">}))</span> <span class="c1"># twice to toggle to dict form</span>
<span class="go">{2: 10, 3: 3}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">factorint</span><span class="p">(</span><span class="n">Mul</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="n">evaluate</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>
<span class="go">{2: 4, 3: 1}</span>
</pre></div>
</div>
<p>The table of the output logic is:</p>
<blockquote>
<div><table class="docutils align-default">
<colgroup>
<col style="width: 23%" />
<col style="width: 23%" />
<col style="width: 27%" />
<col style="width: 27%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"></th>
<th class="head" colspan="3"></th>
</tr>
<tr class="row-even"><th class="head"><p>Input</p></th>
<th class="head"><p>True</p></th>
<th class="head"><p>False</p></th>
<th class="head"><p>other</p></th>
</tr>
</thead>
<tbody>
<tr class="row-odd"><td><p>dict</p></td>
<td><p>mul</p></td>
<td><p>dict</p></td>
<td><p>mul</p></td>
</tr>
<tr class="row-even"><td><p>n</p></td>
<td><p>mul</p></td>
<td><p>dict</p></td>
<td><p>dict</p></td>
</tr>
<tr class="row-odd"><td><p>mul</p></td>
<td><p>mul</p></td>
<td><p>dict</p></td>
<td><p>dict</p></td>
</tr>
</tbody>
</table>
</div></blockquote>
<p class="rubric">Notes</p>
<p>Algorithm:</p>
<p>The function switches between multiple algorithms. Trial division
quickly finds small factors (of the order 1-5 digits), and finds
all large factors if given enough time. The Pollard rho and p-1
algorithms are used to find large factors ahead of time; they
will often find factors of the order of 10 digits within a few
seconds:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">factors</span> <span class="o">=</span> <span class="n">factorint</span><span class="p">(</span><span class="mi">12345678910111213141516</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">base</span><span class="p">,</span> <span class="n">exp</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">factors</span><span class="o">.</span><span class="n">items</span><span class="p">()):</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="n">exp</span><span class="p">))</span>
<span class="gp">...</span>
<span class="go">2 2</span>
<span class="go">2507191691 1</span>
<span class="go">1231026625769 1</span>
</pre></div>
</div>
<p>Any of these methods can optionally be disabled with the following
boolean parameters:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">use_trial</span></code>: Toggle use of trial division</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">use_rho</span></code>: Toggle use of Pollard’s rho method</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">use_pm1</span></code>: Toggle use of Pollard’s p-1 method</p></li>
</ul>
</div></blockquote>
<p><code class="docutils literal notranslate"><span class="pre">factorint</span></code> also periodically checks if the remaining part is
a prime number or a perfect power, and in those cases stops.</p>
<p>For unevaluated factorial, it uses Legendre’s formula(theorem).</p>
<p>If <code class="docutils literal notranslate"><span class="pre">verbose</span></code> is set to <code class="docutils literal notranslate"><span class="pre">True</span></code>, detailed progress is printed.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.smoothness" title="sympy.ntheory.factor_.smoothness"><code class="xref py py-obj docutils literal notranslate"><span class="pre">smoothness</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.smoothness_p" title="sympy.ntheory.factor_.smoothness_p"><code class="xref py py-obj docutils literal notranslate"><span class="pre">smoothness_p</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisors" title="sympy.ntheory.factor_.divisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisors</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.factorrat">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">factorrat</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">rat</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">limit</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_trial</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_rho</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_pm1</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">visual</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">multiple</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L1424-L1484"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.factorrat" title="Permalink to this definition">¶</a></dt>
<dd><p>Given a Rational <code class="docutils literal notranslate"><span class="pre">r</span></code>, <code class="docutils literal notranslate"><span class="pre">factorrat(r)</span></code> returns a dict containing
the prime factors of <code class="docutils literal notranslate"><span class="pre">r</span></code> as keys and their respective multiplicities
as values. For example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">factorrat</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.core.symbol</span> <span class="kn">import</span> <span class="n">S</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">factorrat</span><span class="p">(</span><span class="n">S</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span><span class="o">/</span><span class="mi">9</span><span class="p">)</span>    <span class="c1"># 8/9 = (2**3) * (3**-2)</span>
<span class="go">{2: 3, 3: -2}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">factorrat</span><span class="p">(</span><span class="n">S</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">987</span><span class="p">)</span>    <span class="c1"># -1/789 = -1 * (3**-1) * (7**-1) * (47**-1)</span>
<span class="go">{-1: 1, 3: -1, 7: -1, 47: -1}</span>
</pre></div>
</div>
<p>Please see the docstring for <code class="docutils literal notranslate"><span class="pre">factorint</span></code> for detailed explanations
and examples of the following keywords:</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">limit</span></code>: Integer limit up to which trial division is done</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">use_trial</span></code>: Toggle use of trial division</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">use_rho</span></code>: Toggle use of Pollard’s rho method</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">use_pm1</span></code>: Toggle use of Pollard’s p-1 method</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">verbose</span></code>: Toggle detailed printing of progress</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">multiple</span></code>: Toggle returning a list of factors or dict</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">visual</span></code>: Toggle product form of output</p></li>
</ul>
</div></blockquote>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.primefactors">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">primefactors</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">limit</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L1488-L1525"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.primefactors" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a sorted list of n’s prime factors, ignoring multiplicity
and any composite factor that remains if the limit was set too low
for complete factorization. Unlike factorint(), primefactors() does
not return -1 or 0.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">primefactors</span><span class="p">,</span> <span class="n">factorint</span><span class="p">,</span> <span class="n">isprime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primefactors</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
<span class="go">[2, 3]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primefactors</span><span class="p">(</span><span class="o">-</span><span class="mi">5</span><span class="p">)</span>
<span class="go">[5]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">factorint</span><span class="p">(</span><span class="mi">123456</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
<span class="go">[(2, 6), (3, 1), (643, 1)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primefactors</span><span class="p">(</span><span class="mi">123456</span><span class="p">)</span>
<span class="go">[2, 3, 643]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">factorint</span><span class="p">(</span><span class="mi">10000000001</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">200</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">())</span>
<span class="go">[(101, 1), (99009901, 1)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isprime</span><span class="p">(</span><span class="mi">99009901</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primefactors</span><span class="p">(</span><span class="mi">10000000001</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="mi">300</span><span class="p">)</span>
<span class="go">[101]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.divisors" title="sympy.ntheory.factor_.divisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisors</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.divisors">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">divisors</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">generator</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">proper</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L1553-L1600"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.divisors" title="Permalink to this definition">¶</a></dt>
<dd><p>Return all divisors of n sorted from 1..n by default.
If generator is <code class="docutils literal notranslate"><span class="pre">True</span></code> an unordered generator is returned.</p>
<p>The number of divisors of n can be quite large if there are many
prime factors (counting repeated factors). If only the number of
factors is desired use divisor_count(n).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">divisors</span><span class="p">,</span> <span class="n">divisor_count</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">divisors</span><span class="p">(</span><span class="mi">24</span><span class="p">)</span>
<span class="go">[1, 2, 3, 4, 6, 8, 12, 24]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">divisor_count</span><span class="p">(</span><span class="mi">24</span><span class="p">)</span>
<span class="go">8</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">divisors</span><span class="p">(</span><span class="mi">120</span><span class="p">,</span> <span class="n">generator</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">[1, 2, 4, 8, 3, 6, 12, 24, 5, 10, 20, 40, 15, 30, 60, 120]</span>
</pre></div>
</div>
<p class="rubric">Notes</p>
<p>This is a slightly modified version of Tim Peters referenced at:
<a class="reference external" href="https://stackoverflow.com/questions/1010381/python-factorization">https://stackoverflow.com/questions/1010381/python-factorization</a></p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.primefactors" title="sympy.ntheory.factor_.primefactors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primefactors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisor_count" title="sympy.ntheory.factor_.divisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisor_count</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.proper_divisors">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">proper_divisors</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">generator</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L1641-L1663"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.proper_divisors" title="Permalink to this definition">¶</a></dt>
<dd><p>Return all divisors of n except n, sorted by default.
If generator is <code class="docutils literal notranslate"><span class="pre">True</span></code> an unordered generator is returned.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">proper_divisors</span><span class="p">,</span> <span class="n">proper_divisor_count</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">proper_divisors</span><span class="p">(</span><span class="mi">24</span><span class="p">)</span>
<span class="go">[1, 2, 3, 4, 6, 8, 12]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">proper_divisor_count</span><span class="p">(</span><span class="mi">24</span><span class="p">)</span>
<span class="go">7</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">proper_divisors</span><span class="p">(</span><span class="mi">120</span><span class="p">,</span> <span class="n">generator</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">[1, 2, 4, 8, 3, 6, 12, 24, 5, 10, 20, 40, 15, 30, 60]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisors" title="sympy.ntheory.factor_.divisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.proper_divisor_count" title="sympy.ntheory.factor_.proper_divisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">proper_divisor_count</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.divisor_count">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">divisor_count</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">modulus</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">proper</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L1603-L1638"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.divisor_count" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the number of divisors of <code class="docutils literal notranslate"><span class="pre">n</span></code>. If <code class="docutils literal notranslate"><span class="pre">modulus</span></code> is not 1 then only
those that are divisible by <code class="docutils literal notranslate"><span class="pre">modulus</span></code> are counted. If <code class="docutils literal notranslate"><span class="pre">proper</span></code> is True
then the divisor of <code class="docutils literal notranslate"><span class="pre">n</span></code> will not be counted.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">divisor_count</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">divisor_count</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
<span class="go">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">divisor_count</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">divisor_count</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="n">proper</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">3</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisors" title="sympy.ntheory.factor_.divisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.totient" title="sympy.ntheory.factor_.totient"><code class="xref py py-obj docutils literal notranslate"><span class="pre">totient</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.proper_divisor_count" title="sympy.ntheory.factor_.proper_divisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">proper_divisor_count</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.proper_divisor_count">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">proper_divisor_count</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">modulus</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L1666-L1685"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.proper_divisor_count" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the number of proper divisors of <code class="docutils literal notranslate"><span class="pre">n</span></code>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">proper_divisor_count</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">proper_divisor_count</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
<span class="go">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">proper_divisor_count</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="n">modulus</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="go">1</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.divisors" title="sympy.ntheory.factor_.divisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.proper_divisors" title="sympy.ntheory.factor_.proper_divisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">proper_divisors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisor_count" title="sympy.ntheory.factor_.divisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisor_count</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.udivisors">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">udivisors</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">generator</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L1702-L1746"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.udivisors" title="Permalink to this definition">¶</a></dt>
<dd><p>Return all unitary divisors of n sorted from 1..n by default.
If generator is <code class="docutils literal notranslate"><span class="pre">True</span></code> an unordered generator is returned.</p>
<p>The number of unitary divisors of n can be quite large if there are many
prime factors. If only the number of unitary divisors is desired use
udivisor_count(n).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">udivisors</span><span class="p">,</span> <span class="n">udivisor_count</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">udivisors</span><span class="p">(</span><span class="mi">15</span><span class="p">)</span>
<span class="go">[1, 3, 5, 15]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">udivisor_count</span><span class="p">(</span><span class="mi">15</span><span class="p">)</span>
<span class="go">4</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">udivisors</span><span class="p">(</span><span class="mi">120</span><span class="p">,</span> <span class="n">generator</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">[1, 3, 5, 8, 15, 24, 40, 120]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.primefactors" title="sympy.ntheory.factor_.primefactors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primefactors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisors" title="sympy.ntheory.factor_.divisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisor_count" title="sympy.ntheory.factor_.divisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisor_count</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.udivisor_count" title="sympy.ntheory.factor_.udivisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">udivisor_count</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r562"><span class="brackets"><a class="fn-backref" href="#id11">R562</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Unitary_divisor">https://en.wikipedia.org/wiki/Unitary_divisor</a></p>
</dd>
<dt class="label" id="r563"><span class="brackets"><a class="fn-backref" href="#id12">R563</a></span></dt>
<dd><p><a class="reference external" href="http://mathworld.wolfram.com/UnitaryDivisor.html">http://mathworld.wolfram.com/UnitaryDivisor.html</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.udivisor_count">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">udivisor_count</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L1749-L1779"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.udivisor_count" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the number of unitary divisors of <code class="docutils literal notranslate"><span class="pre">n</span></code>.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>n</strong> : integer</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">udivisor_count</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">udivisor_count</span><span class="p">(</span><span class="mi">120</span><span class="p">)</span>
<span class="go">8</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisors" title="sympy.ntheory.factor_.divisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.udivisors" title="sympy.ntheory.factor_.udivisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">udivisors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisor_count" title="sympy.ntheory.factor_.divisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisor_count</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.totient" title="sympy.ntheory.factor_.totient"><code class="xref py py-obj docutils literal notranslate"><span class="pre">totient</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r564"><span class="brackets"><a class="fn-backref" href="#id13">R564</a></span></dt>
<dd><p><a class="reference external" href="http://mathworld.wolfram.com/UnitaryDivisorFunction.html">http://mathworld.wolfram.com/UnitaryDivisorFunction.html</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.antidivisors">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">antidivisors</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">generator</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L1797-L1832"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.antidivisors" title="Permalink to this definition">¶</a></dt>
<dd><p>Return all antidivisors of n sorted from 1..n by default.</p>
<p>Antidivisors <a class="reference internal" href="#r565" id="id14"><span>[R565]</span></a> of n are numbers that do not divide n by the largest
possible margin.  If generator is True an unordered generator is returned.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">antidivisors</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">antidivisors</span><span class="p">(</span><span class="mi">24</span><span class="p">)</span>
<span class="go">[7, 16]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="n">antidivisors</span><span class="p">(</span><span class="mi">128</span><span class="p">,</span> <span class="n">generator</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
<span class="go">[3, 5, 15, 17, 51, 85]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.primefactors" title="sympy.ntheory.factor_.primefactors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">primefactors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisors" title="sympy.ntheory.factor_.divisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisor_count" title="sympy.ntheory.factor_.divisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisor_count</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.antidivisor_count" title="sympy.ntheory.factor_.antidivisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">antidivisor_count</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r565"><span class="brackets">R565</span><span class="fn-backref">(<a href="#id14">1</a>,<a href="#id15">2</a>)</span></dt>
<dd><p>definition is described in <a class="reference external" href="https://oeis.org/A066272/a066272a.html">https://oeis.org/A066272/a066272a.html</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.antidivisor_count">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">antidivisor_count</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L1835-L1869"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.antidivisor_count" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the number of antidivisors <a class="reference internal" href="#r566" id="id16"><span>[R566]</span></a> of <code class="docutils literal notranslate"><span class="pre">n</span></code>.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>n</strong> : integer</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">antidivisor_count</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">antidivisor_count</span><span class="p">(</span><span class="mi">13</span><span class="p">)</span>
<span class="go">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">antidivisor_count</span><span class="p">(</span><span class="mi">27</span><span class="p">)</span>
<span class="go">5</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisors" title="sympy.ntheory.factor_.divisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.antidivisors" title="sympy.ntheory.factor_.antidivisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">antidivisors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisor_count" title="sympy.ntheory.factor_.divisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisor_count</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.totient" title="sympy.ntheory.factor_.totient"><code class="xref py py-obj docutils literal notranslate"><span class="pre">totient</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r566"><span class="brackets">R566</span><span class="fn-backref">(<a href="#id16">1</a>,<a href="#id17">2</a>)</span></dt>
<dd><p>formula from <a class="reference external" href="https://oeis.org/A066272">https://oeis.org/A066272</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.totient">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">totient</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L1872-L1950"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.totient" title="Permalink to this definition">¶</a></dt>
<dd><p>Calculate the Euler totient function phi(n)</p>
<p><code class="docutils literal notranslate"><span class="pre">totient(n)</span></code> or <span class="math notranslate nohighlight">\(\phi(n)\)</span> is the number of positive integers <span class="math notranslate nohighlight">\(\leq\)</span> n
that are relatively prime to n.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>n</strong> : integer</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">totient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">totient</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">totient</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span>
<span class="go">20</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">totient</span><span class="p">(</span><span class="mi">45</span><span class="p">)</span> <span class="o">==</span> <span class="n">totient</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span><span class="o">*</span><span class="n">totient</span><span class="p">(</span><span class="mi">9</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.divisor_count" title="sympy.ntheory.factor_.divisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisor_count</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r567"><span class="brackets"><a class="fn-backref" href="#id18">R567</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Euler%27s_totient_function">https://en.wikipedia.org/wiki/Euler%27s_totient_function</a></p>
</dd>
<dt class="label" id="r568"><span class="brackets"><a class="fn-backref" href="#id19">R568</a></span></dt>
<dd><p><a class="reference external" href="http://mathworld.wolfram.com/TotientFunction.html">http://mathworld.wolfram.com/TotientFunction.html</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.reduced_totient">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">reduced_totient</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L1953-L2013"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.reduced_totient" title="Permalink to this definition">¶</a></dt>
<dd><p>Calculate the Carmichael reduced totient function lambda(n)</p>
<p><code class="docutils literal notranslate"><span class="pre">reduced_totient(n)</span></code> or <span class="math notranslate nohighlight">\(\lambda(n)\)</span> is the smallest m &gt; 0 such that
<span class="math notranslate nohighlight">\(k^m \equiv 1 \mod n\)</span> for all k relatively prime to n.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">reduced_totient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">reduced_totient</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">reduced_totient</span><span class="p">(</span><span class="mi">8</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">reduced_totient</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
<span class="go">4</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.totient" title="sympy.ntheory.factor_.totient"><code class="xref py py-obj docutils literal notranslate"><span class="pre">totient</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r569"><span class="brackets"><a class="fn-backref" href="#id20">R569</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Carmichael_function">https://en.wikipedia.org/wiki/Carmichael_function</a></p>
</dd>
<dt class="label" id="r570"><span class="brackets"><a class="fn-backref" href="#id21">R570</a></span></dt>
<dd><p><a class="reference external" href="http://mathworld.wolfram.com/CarmichaelFunction.html">http://mathworld.wolfram.com/CarmichaelFunction.html</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.divisor_sigma">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">divisor_sigma</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">k</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L2016-L2100"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.divisor_sigma" title="Permalink to this definition">¶</a></dt>
<dd><p>Calculate the divisor function <span class="math notranslate nohighlight">\(\sigma_k(n)\)</span> for positive integer n</p>
<p><code class="docutils literal notranslate"><span class="pre">divisor_sigma(n,</span> <span class="pre">k)</span></code> is equal to <code class="docutils literal notranslate"><span class="pre">sum([x**k</span> <span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">divisors(n)])</span></code></p>
<p>If n’s prime factorization is:</p>
<div class="math notranslate nohighlight">
\[n = \prod_{i=1}^\omega p_i^{m_i},\]</div>
<p>then</p>
<div class="math notranslate nohighlight">
\[\sigma_k(n) = \prod_{i=1}^\omega (1+p_i^k+p_i^{2k}+\cdots
+ p_i^{m_ik}).\]</div>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>n</strong> : integer</p>
<p><strong>k</strong> : integer, optional</p>
<blockquote>
<div><p>power of divisors in the sum</p>
<p>for k = 0, 1:
<code class="docutils literal notranslate"><span class="pre">divisor_sigma(n,</span> <span class="pre">0)</span></code> is equal to <code class="docutils literal notranslate"><span class="pre">divisor_count(n)</span></code>
<code class="docutils literal notranslate"><span class="pre">divisor_sigma(n,</span> <span class="pre">1)</span></code> is equal to <code class="docutils literal notranslate"><span class="pre">sum(divisors(n))</span></code></p>
<p>Default for k is 1.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">divisor_sigma</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">divisor_sigma</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="go">6</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">divisor_sigma</span><span class="p">(</span><span class="mi">39</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">56</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">divisor_sigma</span><span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">210</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">divisor_sigma</span><span class="p">(</span><span class="mi">37</span><span class="p">)</span>
<span class="go">38</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.divisor_count" title="sympy.ntheory.factor_.divisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisor_count</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.totient" title="sympy.ntheory.factor_.totient"><code class="xref py py-obj docutils literal notranslate"><span class="pre">totient</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisors" title="sympy.ntheory.factor_.divisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r571"><span class="brackets"><a class="fn-backref" href="#id22">R571</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Divisor_function">https://en.wikipedia.org/wiki/Divisor_function</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.udivisor_sigma">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">udivisor_sigma</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">k</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L2170-L2233"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.udivisor_sigma" title="Permalink to this definition">¶</a></dt>
<dd><p>Calculate the unitary divisor function <span class="math notranslate nohighlight">\(\sigma_k^*(n)\)</span> for positive integer n</p>
<p><code class="docutils literal notranslate"><span class="pre">udivisor_sigma(n,</span> <span class="pre">k)</span></code> is equal to <code class="docutils literal notranslate"><span class="pre">sum([x**k</span> <span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">udivisors(n)])</span></code></p>
<p>If n’s prime factorization is:</p>
<div class="math notranslate nohighlight">
\[n = \prod_{i=1}^\omega p_i^{m_i},\]</div>
<p>then</p>
<div class="math notranslate nohighlight">
\[\sigma_k^*(n) = \prod_{i=1}^\omega (1+ p_i^{m_ik}).\]</div>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>k</strong> : power of divisors in the sum</p>
<blockquote>
<div><p>for k = 0, 1:
<code class="docutils literal notranslate"><span class="pre">udivisor_sigma(n,</span> <span class="pre">0)</span></code> is equal to <code class="docutils literal notranslate"><span class="pre">udivisor_count(n)</span></code>
<code class="docutils literal notranslate"><span class="pre">udivisor_sigma(n,</span> <span class="pre">1)</span></code> is equal to <code class="docutils literal notranslate"><span class="pre">sum(udivisors(n))</span></code></p>
<p>Default for k is 1.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">udivisor_sigma</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">udivisor_sigma</span><span class="p">(</span><span class="mi">18</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="go">4</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">udivisor_sigma</span><span class="p">(</span><span class="mi">74</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">114</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">udivisor_sigma</span><span class="p">(</span><span class="mi">36</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">47450</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">udivisor_sigma</span><span class="p">(</span><span class="mi">111</span><span class="p">)</span>
<span class="go">152</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.divisor_count" title="sympy.ntheory.factor_.divisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisor_count</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.totient" title="sympy.ntheory.factor_.totient"><code class="xref py py-obj docutils literal notranslate"><span class="pre">totient</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisors" title="sympy.ntheory.factor_.divisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.udivisors" title="sympy.ntheory.factor_.udivisors"><code class="xref py py-obj docutils literal notranslate"><span class="pre">udivisors</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.udivisor_count" title="sympy.ntheory.factor_.udivisor_count"><code class="xref py py-obj docutils literal notranslate"><span class="pre">udivisor_count</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.divisor_sigma" title="sympy.ntheory.factor_.divisor_sigma"><code class="xref py py-obj docutils literal notranslate"><span class="pre">divisor_sigma</span></code></a>, <a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r572"><span class="brackets"><a class="fn-backref" href="#id23">R572</a></span></dt>
<dd><p><a class="reference external" href="http://mathworld.wolfram.com/UnitaryDivisorFunction.html">http://mathworld.wolfram.com/UnitaryDivisorFunction.html</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.core">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">core</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">t</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">2</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L2103-L2167"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.core" title="Permalink to this definition">¶</a></dt>
<dd><p>Calculate core(n, t) = <span class="math notranslate nohighlight">\(core_t(n)\)</span> of a positive integer n</p>
<p><code class="docutils literal notranslate"><span class="pre">core_2(n)</span></code> is equal to the squarefree part of n</p>
<p>If n’s prime factorization is:</p>
<div class="math notranslate nohighlight">
\[n = \prod_{i=1}^\omega p_i^{m_i},\]</div>
<p>then</p>
<div class="math notranslate nohighlight">
\[core_t(n) = \prod_{i=1}^\omega p_i^{m_i \mod t}.\]</div>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>n</strong> : integer</p>
<p><strong>t</strong> : integer</p>
<blockquote>
<div><p>core(n, t) calculates the t-th power free part of n</p>
<p><code class="docutils literal notranslate"><span class="pre">core(n,</span> <span class="pre">2)</span></code> is the squarefree part of <code class="docutils literal notranslate"><span class="pre">n</span></code>
<code class="docutils literal notranslate"><span class="pre">core(n,</span> <span class="pre">3)</span></code> is the cubefree part of <code class="docutils literal notranslate"><span class="pre">n</span></code></p>
<p>Default for t is 2.</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">core</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">core</span><span class="p">(</span><span class="mi">24</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">6</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">core</span><span class="p">(</span><span class="mi">9424</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">1178</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">core</span><span class="p">(</span><span class="mi">379238</span><span class="p">)</span>
<span class="go">379238</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">core</span><span class="p">(</span><span class="mi">15</span><span class="o">**</span><span class="mi">11</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="go">15</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a>, <a class="reference internal" href="solvers/diophantine.html#sympy.solvers.diophantine.diophantine.square_factor" title="sympy.solvers.diophantine.diophantine.square_factor"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.solvers.diophantine.diophantine.square_factor</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r573"><span class="brackets"><a class="fn-backref" href="#id24">R573</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Square-free_integer#Squarefree_core">https://en.wikipedia.org/wiki/Square-free_integer#Squarefree_core</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.digits">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">digits</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">10</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">digits</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/digits.py#L7-L69"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.digits" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of the digits of <code class="docutils literal notranslate"><span class="pre">n</span></code> in base <code class="docutils literal notranslate"><span class="pre">b</span></code>. The first
element in the list is <code class="docutils literal notranslate"><span class="pre">b</span></code> (or <code class="docutils literal notranslate"><span class="pre">-b</span></code> if <code class="docutils literal notranslate"><span class="pre">n</span></code> is negative).</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>n: integer</strong></p>
<blockquote>
<div><p>The number whose digits are returned.</p>
</div></blockquote>
<p><strong>b: integer</strong></p>
<blockquote>
<div><p>The base in which digits are computed.</p>
</div></blockquote>
<p><strong>digits: integer (or None for all digits)</strong></p>
<blockquote>
<div><p>The number of digits to be returned (padded with zeros, if
necessary).</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.digits</span> <span class="kn">import</span> <span class="n">digits</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">digits</span><span class="p">(</span><span class="mi">35</span><span class="p">)</span>
<span class="go">[10, 3, 5]</span>
</pre></div>
</div>
<p>If the number is negative, the negative sign will be placed on the
base (which is the first element in the returned list):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">digits</span><span class="p">(</span><span class="o">-</span><span class="mi">35</span><span class="p">)</span>
<span class="go">[-10, 3, 5]</span>
</pre></div>
</div>
<p>Bases other than 10 (and greater than 1) can be selected with <code class="docutils literal notranslate"><span class="pre">b</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">digits</span><span class="p">(</span><span class="mi">27</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="go">[2, 1, 1, 0, 1, 1]</span>
</pre></div>
</div>
<p>Use the <code class="docutils literal notranslate"><span class="pre">digits</span></code> keyword if a certain number of digits is desired:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">digits</span><span class="p">(</span><span class="mi">35</span><span class="p">,</span> <span class="n">digits</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="go">[10, 0, 0, 3, 5]</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.primenu">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">primenu</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L2236-L2278"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.primenu" title="Permalink to this definition">¶</a></dt>
<dd><p>Calculate the number of distinct prime factors for a positive integer n.</p>
<p>If n’s prime factorization is:</p>
<div class="math notranslate nohighlight">
\[n = \prod_{i=1}^k p_i^{m_i},\]</div>
<p>then <code class="docutils literal notranslate"><span class="pre">primenu(n)</span></code> or <span class="math notranslate nohighlight">\(\nu(n)\)</span> is:</p>
<div class="math notranslate nohighlight">
\[\nu(n) = k.\]</div>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">primenu</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primenu</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primenu</span><span class="p">(</span><span class="mi">30</span><span class="p">)</span>
<span class="go">3</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r574"><span class="brackets"><a class="fn-backref" href="#id25">R574</a></span></dt>
<dd><p><a class="reference external" href="http://mathworld.wolfram.com/PrimeFactor.html">http://mathworld.wolfram.com/PrimeFactor.html</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.primeomega">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">primeomega</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L2281-L2324"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.primeomega" title="Permalink to this definition">¶</a></dt>
<dd><p>Calculate the number of prime factors counting multiplicities for a
positive integer n.</p>
<p>If n’s prime factorization is:</p>
<div class="math notranslate nohighlight">
\[n = \prod_{i=1}^k p_i^{m_i},\]</div>
<p>then <code class="docutils literal notranslate"><span class="pre">primeomega(n)</span></code>  or <span class="math notranslate nohighlight">\(\Omega(n)\)</span> is:</p>
<div class="math notranslate nohighlight">
\[\Omega(n) = \sum_{i=1}^k m_i.\]</div>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">primeomega</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primeomega</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primeomega</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="go">3</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.factor_.factorint" title="sympy.ntheory.factor_.factorint"><code class="xref py py-obj docutils literal notranslate"><span class="pre">factorint</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r575"><span class="brackets"><a class="fn-backref" href="#id26">R575</a></span></dt>
<dd><p><a class="reference external" href="http://mathworld.wolfram.com/PrimeFactor.html">http://mathworld.wolfram.com/PrimeFactor.html</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.mersenne_prime_exponent">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">mersenne_prime_exponent</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">nth</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L2327-L2345"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.mersenne_prime_exponent" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the exponent <code class="docutils literal notranslate"><span class="pre">i</span></code> for the nth Mersenne prime (which
has the form <span class="math notranslate nohighlight">\(2^i - 1\)</span>).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">mersenne_prime_exponent</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mersenne_prime_exponent</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mersenne_prime_exponent</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="go">4423</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.is_perfect">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">is_perfect</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L2348-L2425"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.is_perfect" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <code class="docutils literal notranslate"><span class="pre">n</span></code> is a perfect number, else False.</p>
<p>A perfect number is equal to the sum of its positive, proper divisors.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">is_perfect</span><span class="p">,</span> <span class="n">divisors</span><span class="p">,</span> <span class="n">divisor_sigma</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_perfect</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_perfect</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">6</span> <span class="o">==</span> <span class="n">divisor_sigma</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span> <span class="o">-</span> <span class="mi">6</span> <span class="o">==</span> <span class="nb">sum</span><span class="p">(</span><span class="n">divisors</span><span class="p">(</span><span class="mi">6</span><span class="p">)[:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="go">True</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r576"><span class="brackets"><a class="fn-backref" href="#id27">R576</a></span></dt>
<dd><p><a class="reference external" href="http://mathworld.wolfram.com/PerfectNumber.html">http://mathworld.wolfram.com/PerfectNumber.html</a></p>
</dd>
<dt class="label" id="r577"><span class="brackets"><a class="fn-backref" href="#id28">R577</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Perfect_number">https://en.wikipedia.org/wiki/Perfect_number</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.is_mersenne_prime">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">is_mersenne_prime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L2428-L2460"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.is_mersenne_prime" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if  <code class="docutils literal notranslate"><span class="pre">n</span></code> is a Mersenne prime, else False.</p>
<p>A Mersenne prime is a prime number having the form <span class="math notranslate nohighlight">\(2^i - 1\)</span>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">is_mersenne_prime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_mersenne_prime</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_mersenne_prime</span><span class="p">(</span><span class="mi">127</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r578"><span class="brackets"><a class="fn-backref" href="#id29">R578</a></span></dt>
<dd><p><a class="reference external" href="http://mathworld.wolfram.com/MersennePrime.html">http://mathworld.wolfram.com/MersennePrime.html</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.abundance">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">abundance</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L2463-L2480"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.abundance" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the difference between the sum of the positive
proper divisors of a number and the number.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">abundance</span><span class="p">,</span> <span class="n">is_perfect</span><span class="p">,</span> <span class="n">is_abundant</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">abundance</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_perfect</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">abundance</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">-2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_abundant</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.is_abundant">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">is_abundant</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L2483-L2506"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.is_abundant" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <code class="docutils literal notranslate"><span class="pre">n</span></code> is an abundant number, else False.</p>
<p>A abundant number is smaller than the sum of its positive proper divisors.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">is_abundant</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_abundant</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_abundant</span><span class="p">(</span><span class="mi">15</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r579"><span class="brackets"><a class="fn-backref" href="#id30">R579</a></span></dt>
<dd><p><a class="reference external" href="http://mathworld.wolfram.com/AbundantNumber.html">http://mathworld.wolfram.com/AbundantNumber.html</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.is_deficient">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">is_deficient</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L2509-L2532"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.is_deficient" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <code class="docutils literal notranslate"><span class="pre">n</span></code> is a deficient number, else False.</p>
<p>A deficient number is greater than the sum of its positive proper divisors.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">is_deficient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_deficient</span><span class="p">(</span><span class="mi">20</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_deficient</span><span class="p">(</span><span class="mi">15</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r580"><span class="brackets"><a class="fn-backref" href="#id31">R580</a></span></dt>
<dd><p><a class="reference external" href="http://mathworld.wolfram.com/DeficientNumber.html">http://mathworld.wolfram.com/DeficientNumber.html</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.factor_.is_amicable">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.factor_.</span></span><span class="sig-name descname"><span class="pre">is_amicable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">m</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/factor_.py#L2535-L2559"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.factor_.is_amicable" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if the numbers <span class="math notranslate nohighlight">\(m\)</span> and <span class="math notranslate nohighlight">\(n\)</span> are “amicable”, else False.</p>
<p>Amicable numbers are two different numbers so related that the sum
of the proper divisors of each is equal to that of the other.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.factor_</span> <span class="kn">import</span> <span class="n">is_amicable</span><span class="p">,</span> <span class="n">divisor_sigma</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_amicable</span><span class="p">(</span><span class="mi">220</span><span class="p">,</span> <span class="mi">284</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">divisor_sigma</span><span class="p">(</span><span class="mi">220</span><span class="p">)</span> <span class="o">==</span> <span class="n">divisor_sigma</span><span class="p">(</span><span class="mi">284</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r581"><span class="brackets"><a class="fn-backref" href="#id32">R581</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Amicable_numbers">https://en.wikipedia.org/wiki/Amicable_numbers</a></p>
</dd>
</dl>
</dd></dl>

<span class="target" id="module-sympy.ntheory.modular"></span><dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.modular.symmetric_residue">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.modular.</span></span><span class="sig-name descname"><span class="pre">symmetric_residue</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">m</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/modular.py#L11-L22"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.modular.symmetric_residue" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the residual mod m such that it is within half of the modulus.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.modular</span> <span class="kn">import</span> <span class="n">symmetric_residue</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">symmetric_residue</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">symmetric_residue</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
<span class="go">-2</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.modular.crt">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.modular.</span></span><span class="sig-name descname"><span class="pre">crt</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">m</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">symmetric</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/modular.py#L25-L100"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.modular.crt" title="Permalink to this definition">¶</a></dt>
<dd><p>Chinese Remainder Theorem.</p>
<p>The moduli in m are assumed to be pairwise coprime.  The output
is then an integer f, such that f = v_i mod m_i for each pair out
of v and m. If <code class="docutils literal notranslate"><span class="pre">symmetric</span></code> is False a positive integer will be
returned, else |f| will be less than or equal to the LCM of the
moduli, and thus f may be negative.</p>
<p>If the moduli are not co-prime the correct result will be returned
if/when the test of the result is found to be incorrect. This result
will be None if there is no solution.</p>
<p>The keyword <code class="docutils literal notranslate"><span class="pre">check</span></code> can be set to False if it is known that the moduli
are coprime.</p>
<p class="rubric">Examples</p>
<p>As an example consider a set of residues <code class="docutils literal notranslate"><span class="pre">U</span> <span class="pre">=</span> <span class="pre">[49,</span> <span class="pre">76,</span> <span class="pre">65]</span></code>
and a set of moduli <code class="docutils literal notranslate"><span class="pre">M</span> <span class="pre">=</span> <span class="pre">[99,</span> <span class="pre">97,</span> <span class="pre">95]</span></code>. Then we have:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.modular</span> <span class="kn">import</span> <span class="n">crt</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">crt</span><span class="p">([</span><span class="mi">99</span><span class="p">,</span> <span class="mi">97</span><span class="p">,</span> <span class="mi">95</span><span class="p">],</span> <span class="p">[</span><span class="mi">49</span><span class="p">,</span> <span class="mi">76</span><span class="p">,</span> <span class="mi">65</span><span class="p">])</span>
<span class="go">(639985, 912285)</span>
</pre></div>
</div>
<p>This is the correct result because:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="mi">639985</span> <span class="o">%</span> <span class="n">m</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">99</span><span class="p">,</span> <span class="mi">97</span><span class="p">,</span> <span class="mi">95</span><span class="p">]]</span>
<span class="go">[49, 76, 65]</span>
</pre></div>
</div>
<p>If the moduli are not co-prime, you may receive an incorrect result
if you use <code class="docutils literal notranslate"><span class="pre">check=False</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">crt</span><span class="p">([</span><span class="mi">12</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">17</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="n">check</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="go">(954, 1224)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="mi">954</span> <span class="o">%</span> <span class="n">m</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">12</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">17</span><span class="p">]]</span>
<span class="go">[6, 0, 2]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">crt</span><span class="p">([</span><span class="mi">12</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">17</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span> <span class="ow">is</span> <span class="kc">None</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">crt</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span> <span class="mi">6</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span>
<span class="go">(5, 6)</span>
</pre></div>
</div>
<p>Note: the order of gf_crt’s arguments is reversed relative to crt,
and that solve_congruence takes residue, modulus pairs.</p>
<p>Programmer’s note: rather than checking that all pairs of moduli share
no GCD (an O(n**2) test) and rather than factoring all moduli and seeing
that there is no factor in common, a check that the result gives the
indicated residuals is performed – an O(n) operation.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.modular.solve_congruence" title="sympy.ntheory.modular.solve_congruence"><code class="xref py py-obj docutils literal notranslate"><span class="pre">solve_congruence</span></code></a></p>
<dl class="simple">
<dt><a class="reference internal" href="polys/internals.html#sympy.polys.galoistools.gf_crt" title="sympy.polys.galoistools.gf_crt"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.polys.galoistools.gf_crt</span></code></a></dt><dd><p>low level crt routine used by this routine</p>
</dd>
</dl>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.modular.crt1">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.modular.</span></span><span class="sig-name descname"><span class="pre">crt1</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">m</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/modular.py#L103-L114"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.modular.crt1" title="Permalink to this definition">¶</a></dt>
<dd><p>First part of Chinese Remainder Theorem, for multiple application.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.modular</span> <span class="kn">import</span> <span class="n">crt1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">crt1</span><span class="p">([</span><span class="mi">18</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="mi">6</span><span class="p">])</span>
<span class="go">(4536, [252, 108, 756], [0, 2, 0])</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.modular.crt2">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.modular.</span></span><span class="sig-name descname"><span class="pre">crt2</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">m</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">v</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mm</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">e</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">symmetric</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/modular.py#L117-L133"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.modular.crt2" title="Permalink to this definition">¶</a></dt>
<dd><p>Second part of Chinese Remainder Theorem, for multiple application.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.modular</span> <span class="kn">import</span> <span class="n">crt1</span><span class="p">,</span> <span class="n">crt2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mm</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">s</span> <span class="o">=</span> <span class="n">crt1</span><span class="p">([</span><span class="mi">18</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="mi">6</span><span class="p">])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">crt2</span><span class="p">([</span><span class="mi">18</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="mi">6</span><span class="p">],</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">mm</span><span class="p">,</span> <span class="n">e</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span>
<span class="go">(0, 4536)</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.modular.solve_congruence">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.modular.</span></span><span class="sig-name descname"><span class="pre">solve_congruence</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">remainder_modulus_pairs</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">hint</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/modular.py#L136-L255"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.modular.solve_congruence" title="Permalink to this definition">¶</a></dt>
<dd><p>Compute the integer <code class="docutils literal notranslate"><span class="pre">n</span></code> that has the residual <code class="docutils literal notranslate"><span class="pre">ai</span></code> when it is
divided by <code class="docutils literal notranslate"><span class="pre">mi</span></code> where the <code class="docutils literal notranslate"><span class="pre">ai</span></code> and <code class="docutils literal notranslate"><span class="pre">mi</span></code> are given as pairs to
this function: ((a1, m1), (a2, m2), …). If there is no solution,
return None. Otherwise return <code class="docutils literal notranslate"><span class="pre">n</span></code> and its modulus.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">mi</span></code> values need not be co-prime. If it is known that the moduli are
not co-prime then the hint <code class="docutils literal notranslate"><span class="pre">check</span></code> can be set to False (default=True) and
the check for a quicker solution via crt() (valid when the moduli are
co-prime) will be skipped.</p>
<p>If the hint <code class="docutils literal notranslate"><span class="pre">symmetric</span></code> is True (default is False), the value of <code class="docutils literal notranslate"><span class="pre">n</span></code>
will be within 1/2 of the modulus, possibly negative.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.modular</span> <span class="kn">import</span> <span class="n">solve_congruence</span>
</pre></div>
</div>
<p>What number is 2 mod 3, 3 mod 5 and 2 mod 7?</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">solve_congruence</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">7</span><span class="p">))</span>
<span class="go">(23, 105)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="mi">23</span> <span class="o">%</span> <span class="n">m</span> <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">]]</span>
<span class="go">[2, 3, 2]</span>
</pre></div>
</div>
<p>If you prefer to work with all remainder in one list and
all moduli in another, send the arguments like this:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">solve_congruence</span><span class="p">(</span><span class="o">*</span><span class="nb">zip</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">7</span><span class="p">)))</span>
<span class="go">(23, 105)</span>
</pre></div>
</div>
<p>The moduli need not be co-prime; in this case there may or
may not be a solution:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">solve_congruence</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span> <span class="ow">is</span> <span class="kc">None</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">solve_congruence</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">))</span>
<span class="go">(5, 6)</span>
</pre></div>
</div>
<p>The symmetric flag will make the result be within 1/2 of the modulus:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">solve_congruence</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">),</span> <span class="n">symmetric</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="go">(-1, 6)</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#sympy.ntheory.modular.crt" title="sympy.ntheory.modular.crt"><code class="xref py py-obj docutils literal notranslate"><span class="pre">crt</span></code></a></dt><dd><p>high level routine implementing the Chinese Remainder Theorem</p>
</dd>
</dl>
</div>
</dd></dl>

<span class="target" id="module-sympy.ntheory.multinomial"></span><dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.multinomial.binomial_coefficients">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.multinomial.</span></span><span class="sig-name descname"><span class="pre">binomial_coefficients</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/multinomial.py#L4-L27"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.multinomial.binomial_coefficients" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a dictionary containing pairs <span class="math notranslate nohighlight">\({(k1,k2) : C_kn}\)</span> where
<span class="math notranslate nohighlight">\(C_kn\)</span> are binomial coefficients and <span class="math notranslate nohighlight">\(n=k1+k2\)</span>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">binomial_coefficients</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">binomial_coefficients</span><span class="p">(</span><span class="mi">9</span><span class="p">)</span>
<span class="go">{(0, 9): 1, (1, 8): 9, (2, 7): 36, (3, 6): 84,</span>
<span class="go"> (4, 5): 126, (5, 4): 126, (6, 3): 84, (7, 2): 36, (8, 1): 9, (9, 0): 1}</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.multinomial.binomial_coefficients_list" title="sympy.ntheory.multinomial.binomial_coefficients_list"><code class="xref py py-obj docutils literal notranslate"><span class="pre">binomial_coefficients_list</span></code></a>, <a class="reference internal" href="#sympy.ntheory.multinomial.multinomial_coefficients" title="sympy.ntheory.multinomial.multinomial_coefficients"><code class="xref py py-obj docutils literal notranslate"><span class="pre">multinomial_coefficients</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.multinomial.binomial_coefficients_list">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.multinomial.</span></span><span class="sig-name descname"><span class="pre">binomial_coefficients_list</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/multinomial.py#L30-L52"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.multinomial.binomial_coefficients_list" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of binomial coefficients as rows of the Pascal’s
triangle.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">binomial_coefficients_list</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">binomial_coefficients_list</span><span class="p">(</span><span class="mi">9</span><span class="p">)</span>
<span class="go">[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.multinomial.binomial_coefficients" title="sympy.ntheory.multinomial.binomial_coefficients"><code class="xref py py-obj docutils literal notranslate"><span class="pre">binomial_coefficients</span></code></a>, <a class="reference internal" href="#sympy.ntheory.multinomial.multinomial_coefficients" title="sympy.ntheory.multinomial.multinomial_coefficients"><code class="xref py py-obj docutils literal notranslate"><span class="pre">multinomial_coefficients</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.multinomial.multinomial_coefficients">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.multinomial.</span></span><span class="sig-name descname"><span class="pre">multinomial_coefficients</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">m</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/multinomial.py#L55-L126"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.multinomial.multinomial_coefficients" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a dictionary containing pairs <code class="docutils literal notranslate"><span class="pre">{(k1,k2,..,km)</span> <span class="pre">:</span> <span class="pre">C_kn}</span></code>
where <code class="docutils literal notranslate"><span class="pre">C_kn</span></code> are multinomial coefficients such that
<code class="docutils literal notranslate"><span class="pre">n=k1+k2+..+km</span></code>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">multinomial_coefficients</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">multinomial_coefficients</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span> <span class="c1"># indirect doctest</span>
<span class="go">{(0, 5): 1, (1, 4): 5, (2, 3): 10, (3, 2): 10, (4, 1): 5, (5, 0): 1}</span>
</pre></div>
</div>
<p class="rubric">Notes</p>
<p>The algorithm is based on the following result:</p>
<div class="math notranslate nohighlight">
\[\binom{n}{k_1, \ldots, k_m} =
\frac{k_1 + 1}{n - k_1} \sum_{i=2}^m \binom{n}{k_1 + 1, \ldots, k_i - 1, \ldots}\]</div>
<p>Code contributed to Sage by Yann Laigle-Chapuy, copied with permission
of the author.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.multinomial.binomial_coefficients_list" title="sympy.ntheory.multinomial.binomial_coefficients_list"><code class="xref py py-obj docutils literal notranslate"><span class="pre">binomial_coefficients_list</span></code></a>, <a class="reference internal" href="#sympy.ntheory.multinomial.binomial_coefficients" title="sympy.ntheory.multinomial.binomial_coefficients"><code class="xref py py-obj docutils literal notranslate"><span class="pre">binomial_coefficients</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.multinomial.multinomial_coefficients_iterator">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.multinomial.</span></span><span class="sig-name descname"><span class="pre">multinomial_coefficients_iterator</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">m</span></em>, <em class="sig-param"><span class="pre">n</span></em>, <em class="sig-param"><span class="pre">_tuple=&lt;class</span> <span class="pre">'tuple'&gt;</span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/multinomial.py#L129-L188"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.multinomial.multinomial_coefficients_iterator" title="Permalink to this definition">¶</a></dt>
<dd><p>multinomial coefficient iterator</p>
<p>This routine has been optimized for <span class="math notranslate nohighlight">\(m\)</span> large with respect to <span class="math notranslate nohighlight">\(n\)</span> by taking
advantage of the fact that when the monomial tuples <span class="math notranslate nohighlight">\(t\)</span> are stripped of
zeros, their coefficient is the same as that of the monomial tuples from
<code class="docutils literal notranslate"><span class="pre">multinomial_coefficients(n,</span> <span class="pre">n)</span></code>. Therefore, the latter coefficients are
precomputed to save memory and time.</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.multinomial</span> <span class="kn">import</span> <span class="n">multinomial_coefficients</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m53</span><span class="p">,</span> <span class="n">m33</span> <span class="o">=</span> <span class="n">multinomial_coefficients</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">multinomial_coefficients</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">m53</span><span class="p">[(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)]</span> <span class="o">==</span> <span class="n">m53</span><span class="p">[(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">)]</span> <span class="o">==</span> <span class="n">m53</span><span class="p">[(</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">)]</span> <span class="o">==</span> <span class="n">m33</span><span class="p">[(</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)]</span>
<span class="go">True</span>
</pre></div>
</div>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.multinomial</span> <span class="kn">import</span> <span class="n">multinomial_coefficients_iterator</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">it</span> <span class="o">=</span> <span class="n">multinomial_coefficients_iterator</span><span class="p">(</span><span class="mi">20</span><span class="p">,</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">)</span>
<span class="go">((3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 1)</span>
</pre></div>
</div>
</dd></dl>

<span class="target" id="module-sympy.ntheory.partitions_"></span><dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.partitions_.npartitions">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.partitions_.</span></span><span class="sig-name descname"><span class="pre">npartitions</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">verbose</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/partitions_.py#L138-L190"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.partitions_.npartitions" title="Permalink to this definition">¶</a></dt>
<dd><p>Calculate the partition function P(n), i.e. the number of ways that
n can be written as a sum of positive integers.</p>
<p>P(n) is computed using the Hardy-Ramanujan-Rademacher formula <a class="reference internal" href="#r582" id="id33"><span>[R582]</span></a>.</p>
<p>The correctness of this implementation has been tested through 10**10.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">npartitions</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">npartitions</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span>
<span class="go">1958</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r582"><span class="brackets">R582</span><span class="fn-backref">(<a href="#id33">1</a>,<a href="#id34">2</a>)</span></dt>
<dd><p><a class="reference external" href="http://mathworld.wolfram.com/PartitionFunctionP.html">http://mathworld.wolfram.com/PartitionFunctionP.html</a></p>
</dd>
</dl>
</dd></dl>

<span class="target" id="module-sympy.ntheory.primetest"></span><dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.primetest.is_euler_pseudoprime">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.primetest.</span></span><span class="sig-name descname"><span class="pre">is_euler_pseudoprime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/primetest.py#L15-L56"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.primetest.is_euler_pseudoprime" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if n is prime or an Euler pseudoprime to base b, else False.</p>
<p>Euler Pseudoprime : In arithmetic, an odd composite integer n is called an
euler pseudoprime to base a, if a and n are coprime and satisfy the modular
arithmetic congruence relation :</p>
<p>a ^ (n-1)/2 = + 1(mod n) or
a ^ (n-1)/2 = - 1(mod n)</p>
<p>(where mod refers to the modulo operation).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.primetest</span> <span class="kn">import</span> <span class="n">is_euler_pseudoprime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_euler_pseudoprime</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r583"><span class="brackets"><a class="fn-backref" href="#id35">R583</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Euler_pseudoprime">https://en.wikipedia.org/wiki/Euler_pseudoprime</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.primetest.is_square">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.primetest.</span></span><span class="sig-name descname"><span class="pre">is_square</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prep</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/primetest.py#L59-L94"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.primetest.is_square" title="Permalink to this definition">¶</a></dt>
<dd><p>Return True if n == a * a for some integer a, else False.
If n is suspected of <em>not</em> being a square then this is a
quick method of confirming that it is not.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.primetest</span> <span class="kn">import</span> <span class="n">is_square</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_square</span><span class="p">(</span><span class="mi">25</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_square</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="core.html#sympy.core.power.integer_nthroot" title="sympy.core.power.integer_nthroot"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.core.power.integer_nthroot</span></code></a></p>
</div>
<p class="rubric">References</p>
<p>[1]  <a class="reference external" href="http://mersenneforum.org/showpost.php?p=110896">http://mersenneforum.org/showpost.php?p=110896</a></p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.primetest.mr">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.primetest.</span></span><span class="sig-name descname"><span class="pre">mr</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">bases</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/primetest.py#L118-L158"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.primetest.mr" title="Permalink to this definition">¶</a></dt>
<dd><p>Perform a Miller-Rabin strong pseudoprime test on n using a
given list of bases/witnesses.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.primetest</span> <span class="kn">import</span> <span class="n">mr</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mr</span><span class="p">(</span><span class="mi">1373651</span><span class="p">,</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mr</span><span class="p">(</span><span class="mi">479001599</span><span class="p">,</span> <span class="p">[</span><span class="mi">31</span><span class="p">,</span> <span class="mi">73</span><span class="p">])</span>
<span class="go">True</span>
</pre></div>
</div>
<p class="rubric">References</p>
<ul class="simple">
<li><p>Richard Crandall &amp; Carl Pomerance (2005), “Prime Numbers:
A Computational Perspective”, Springer, 2nd edition, 135-138</p></li>
</ul>
<p>A list of thresholds and the bases they require are here:
<a class="reference external" href="https://en.wikipedia.org/wiki/Miller–Rabin_primality_test#Deterministic_variants">https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test#Deterministic_variants</a></p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.primetest.is_lucas_prp">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.primetest.</span></span><span class="sig-name descname"><span class="pre">is_lucas_prp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/primetest.py#L293-L337"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.primetest.is_lucas_prp" title="Permalink to this definition">¶</a></dt>
<dd><p>Standard Lucas compositeness test with Selfridge parameters.  Returns
False if n is definitely composite, and True if n is a Lucas probable
prime.</p>
<p>This is typically used in combination with the Miller-Rabin test.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.primetest</span> <span class="kn">import</span> <span class="n">isprime</span><span class="p">,</span> <span class="n">is_lucas_prp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10000</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">if</span> <span class="n">is_lucas_prp</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">isprime</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="gp">... </span>       <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="go">323</span>
<span class="go">377</span>
<span class="go">1159</span>
<span class="go">1829</span>
<span class="go">3827</span>
<span class="go">5459</span>
<span class="go">5777</span>
<span class="go">9071</span>
<span class="go">9179</span>
</pre></div>
</div>
<p class="rubric">References</p>
<ul class="simple">
<li><p>“Lucas Pseudoprimes”, Baillie and Wagstaff, 1980.
<a class="reference external" href="http://mpqs.free.fr/LucasPseudoprimes.pdf">http://mpqs.free.fr/LucasPseudoprimes.pdf</a></p></li>
<li><p>OEIS A217120: Lucas Pseudoprimes
<a class="reference external" href="https://oeis.org/A217120">https://oeis.org/A217120</a></p></li>
<li><p><a class="reference external" href="https://en.wikipedia.org/wiki/Lucas_pseudoprime">https://en.wikipedia.org/wiki/Lucas_pseudoprime</a></p></li>
</ul>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.primetest.is_strong_lucas_prp">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.primetest.</span></span><span class="sig-name descname"><span class="pre">is_strong_lucas_prp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/primetest.py#L340-L396"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.primetest.is_strong_lucas_prp" title="Permalink to this definition">¶</a></dt>
<dd><p>Strong Lucas compositeness test with Selfridge parameters.  Returns
False if n is definitely composite, and True if n is a strong Lucas
probable prime.</p>
<p>This is often used in combination with the Miller-Rabin test, and
in particular, when combined with M-R base 2 creates the strong BPSW test.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.primetest</span> <span class="kn">import</span> <span class="n">isprime</span><span class="p">,</span> <span class="n">is_strong_lucas_prp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">20000</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">if</span> <span class="n">is_strong_lucas_prp</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">isprime</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="gp">... </span>       <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="go">5459</span>
<span class="go">5777</span>
<span class="go">10877</span>
<span class="go">16109</span>
<span class="go">18971</span>
</pre></div>
</div>
<p class="rubric">References</p>
<ul class="simple">
<li><p>“Lucas Pseudoprimes”, Baillie and Wagstaff, 1980.
<a class="reference external" href="http://mpqs.free.fr/LucasPseudoprimes.pdf">http://mpqs.free.fr/LucasPseudoprimes.pdf</a></p></li>
<li><p>OEIS A217255: Strong Lucas Pseudoprimes
<a class="reference external" href="https://oeis.org/A217255">https://oeis.org/A217255</a></p></li>
<li><p><a class="reference external" href="https://en.wikipedia.org/wiki/Lucas_pseudoprime">https://en.wikipedia.org/wiki/Lucas_pseudoprime</a></p></li>
<li><p><a class="reference external" href="https://en.wikipedia.org/wiki/Baillie-PSW_primality_test">https://en.wikipedia.org/wiki/Baillie-PSW_primality_test</a></p></li>
</ul>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.primetest.is_extra_strong_lucas_prp">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.primetest.</span></span><span class="sig-name descname"><span class="pre">is_extra_strong_lucas_prp</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/primetest.py#L399-L469"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.primetest.is_extra_strong_lucas_prp" title="Permalink to this definition">¶</a></dt>
<dd><p>Extra Strong Lucas compositeness test.  Returns False if n is
definitely composite, and True if n is a “extra strong” Lucas probable
prime.</p>
<p>The parameters are selected using P = 3, Q = 1, then incrementing P until
(D|n) == -1.  The test itself is as defined in Grantham 2000, from the
Mo and Jones preprint.  The parameter selection and test are the same as
used in OEIS A217719, Perl’s Math::Prime::Util, and the Lucas pseudoprime
page on Wikipedia.</p>
<p>With these parameters, there are no counterexamples below 2^64 nor any
known above that range.  It is 20-50% faster than the strong test.</p>
<p>Because of the different parameters selected, there is no relationship
between the strong Lucas pseudoprimes and extra strong Lucas pseudoprimes.
In particular, one is not a subset of the other.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.primetest</span> <span class="kn">import</span> <span class="n">isprime</span><span class="p">,</span> <span class="n">is_extra_strong_lucas_prp</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">20000</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">if</span> <span class="n">is_extra_strong_lucas_prp</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">isprime</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="gp">... </span>       <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="go">989</span>
<span class="go">3239</span>
<span class="go">5777</span>
<span class="go">10877</span>
</pre></div>
</div>
<p class="rubric">References</p>
<ul class="simple">
<li><p>“Frobenius Pseudoprimes”, Jon Grantham, 2000.
<a class="reference external" href="http://www.ams.org/journals/mcom/2001-70-234/S0025-5718-00-01197-2/">http://www.ams.org/journals/mcom/2001-70-234/S0025-5718-00-01197-2/</a></p></li>
<li><p>OEIS A217719: Extra Strong Lucas Pseudoprimes
<a class="reference external" href="https://oeis.org/A217719">https://oeis.org/A217719</a></p></li>
<li><p><a class="reference external" href="https://en.wikipedia.org/wiki/Lucas_pseudoprime">https://en.wikipedia.org/wiki/Lucas_pseudoprime</a></p></li>
</ul>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.primetest.isprime">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.primetest.</span></span><span class="sig-name descname"><span class="pre">isprime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/primetest.py#L472-L641"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.primetest.isprime" title="Permalink to this definition">¶</a></dt>
<dd><p>Test if n is a prime number (True) or not (False). For n &lt; 2^64 the
answer is definitive; larger n values have a small probability of actually
being pseudoprimes.</p>
<p>Negative numbers (e.g. -2) are not considered prime.</p>
<p>The first step is looking for trivial factors, which if found enables
a quick return.  Next, if the sieve is large enough, use bisection search
on the sieve.  For small numbers, a set of deterministic Miller-Rabin
tests are performed with bases that are known to have no counterexamples
in their range.  Finally if the number is larger than 2^64, a strong
BPSW test is performed.  While this is a probable prime test and we
believe counterexamples exist, there are no known counterexamples.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">isprime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isprime</span><span class="p">(</span><span class="mi">13</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isprime</span><span class="p">(</span><span class="mf">13.0</span><span class="p">)</span>  <span class="c1"># limited precision</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">isprime</span><span class="p">(</span><span class="mi">15</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<p class="rubric">Notes</p>
<p>This routine is intended only for integer input, not numerical
expressions which may represent numbers. Floats are also
rejected as input because they represent numbers of limited
precision. While it is tempting to permit 7.0 to represent an
integer there are errors that may “pass silently” if this is
allowed:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Float</span><span class="p">,</span> <span class="n">S</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span><span class="p">(</span><span class="mf">1e3</span><span class="p">)</span> <span class="o">==</span> <span class="mf">1e3</span> <span class="o">==</span> <span class="mi">10</span><span class="o">**</span><span class="mi">3</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span><span class="p">(</span><span class="mf">1e23</span><span class="p">)</span> <span class="o">==</span> <span class="mf">1e23</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span><span class="p">(</span><span class="mf">1e23</span><span class="p">)</span> <span class="o">==</span> <span class="mi">10</span><span class="o">**</span><span class="mi">23</span>
<span class="go">False</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">near_int</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="n">S</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">/</span><span class="mi">10</span><span class="o">**</span><span class="mi">19</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">near_int</span> <span class="o">==</span> <span class="nb">int</span><span class="p">(</span><span class="n">near_int</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">Float</span><span class="p">(</span><span class="n">near_int</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>  <span class="c1"># truncated by precision</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">==</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="n">Float</span><span class="p">(</span><span class="n">near_int</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">==</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="#sympy.ntheory.generate.primerange" title="sympy.ntheory.generate.primerange"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.ntheory.generate.primerange</span></code></a></dt><dd><p>Generates all primes in a given range</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.primepi" title="sympy.ntheory.generate.primepi"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.ntheory.generate.primepi</span></code></a></dt><dd><p>Return the number of primes less than or equal to n</p>
</dd>
<dt><a class="reference internal" href="#sympy.ntheory.generate.prime" title="sympy.ntheory.generate.prime"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.ntheory.generate.prime</span></code></a></dt><dd><p>Return the nth prime</p>
</dd>
</dl>
</div>
<p class="rubric">References</p>
<ul class="simple">
<li><p><a class="reference external" href="https://en.wikipedia.org/wiki/Strong_pseudoprime">https://en.wikipedia.org/wiki/Strong_pseudoprime</a></p></li>
<li><p>“Lucas Pseudoprimes”, Baillie and Wagstaff, 1980.
<a class="reference external" href="http://mpqs.free.fr/LucasPseudoprimes.pdf">http://mpqs.free.fr/LucasPseudoprimes.pdf</a></p></li>
<li><p><a class="reference external" href="https://en.wikipedia.org/wiki/Baillie-PSW_primality_test">https://en.wikipedia.org/wiki/Baillie-PSW_primality_test</a></p></li>
</ul>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.primetest.is_gaussian_prime">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.primetest.</span></span><span class="sig-name descname"><span class="pre">is_gaussian_prime</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">num</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/primetest.py#L644-L664"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.primetest.is_gaussian_prime" title="Permalink to this definition">¶</a></dt>
<dd><p>Test if num is a Gaussian prime number.</p>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r584"><span class="brackets"><a class="fn-backref" href="#id36">R584</a></span></dt>
<dd><p><a class="reference external" href="https://oeis.org/wiki/Gaussian_primes">https://oeis.org/wiki/Gaussian_primes</a></p>
</dd>
</dl>
</dd></dl>

<span class="target" id="module-sympy.ntheory.residue_ntheory"></span><dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.residue_ntheory.n_order">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.residue_ntheory.</span></span><span class="sig-name descname"><span class="pre">n_order</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/residue_ntheory.py#L13-L53"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.residue_ntheory.n_order" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the order of <code class="docutils literal notranslate"><span class="pre">a</span></code> modulo <code class="docutils literal notranslate"><span class="pre">n</span></code>.</p>
<p>The order of <code class="docutils literal notranslate"><span class="pre">a</span></code> modulo <code class="docutils literal notranslate"><span class="pre">n</span></code> is the smallest integer
<code class="docutils literal notranslate"><span class="pre">k</span></code> such that <code class="docutils literal notranslate"><span class="pre">a**k</span></code> leaves a remainder of 1 with <code class="docutils literal notranslate"><span class="pre">n</span></code>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">n_order</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n_order</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span>
<span class="go">6</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n_order</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span>
<span class="go">3</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.residue_ntheory.is_primitive_root">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.residue_ntheory.</span></span><span class="sig-name descname"><span class="pre">is_primitive_root</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">p</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/residue_ntheory.py#L152-L180"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.residue_ntheory.is_primitive_root" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <code class="docutils literal notranslate"><span class="pre">a</span></code> is a primitive root of <code class="docutils literal notranslate"><span class="pre">p</span></code></p>
<p><code class="docutils literal notranslate"><span class="pre">a</span></code> is said to be the primitive root of <code class="docutils literal notranslate"><span class="pre">p</span></code> if gcd(a, p) == 1 and
totient(p) is the smallest positive number s.t.</p>
<blockquote>
<div><p>a**totient(p) cong 1 mod(p)</p>
</div></blockquote>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">is_primitive_root</span><span class="p">,</span> <span class="n">n_order</span><span class="p">,</span> <span class="n">totient</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_primitive_root</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">is_primitive_root</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n_order</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span> <span class="o">==</span> <span class="n">totient</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">n_order</span><span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span> <span class="o">==</span> <span class="n">totient</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.residue_ntheory.primitive_root">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.residue_ntheory.</span></span><span class="sig-name descname"><span class="pre">primitive_root</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">p</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/residue_ntheory.py#L86-L149"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.residue_ntheory.primitive_root" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the smallest primitive root or None</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>p</strong> : positive integer</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.residue_ntheory</span> <span class="kn">import</span> <span class="n">primitive_root</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">primitive_root</span><span class="p">(</span><span class="mi">19</span><span class="p">)</span>
<span class="go">2</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r585"><span class="brackets"><a class="fn-backref" href="#id37">R585</a></span></dt>
<dd><ol class="upperalpha simple" start="23">
<li><p>Stein “Elementary Number Theory” (2011), page 44</p></li>
</ol>
</dd>
<dt class="label" id="r586"><span class="brackets"><a class="fn-backref" href="#id38">R586</a></span></dt>
<dd><ol class="upperalpha simple" start="16">
<li><p>Hackman “Elementary Number Theory” (2009), Chapter C</p></li>
</ol>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.residue_ntheory.sqrt_mod">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.residue_ntheory.</span></span><span class="sig-name descname"><span class="pre">sqrt_mod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">p</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">all_roots</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/residue_ntheory.py#L215-L264"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.residue_ntheory.sqrt_mod" title="Permalink to this definition">¶</a></dt>
<dd><p>Find a root of <code class="docutils literal notranslate"><span class="pre">x**2</span> <span class="pre">=</span> <span class="pre">a</span> <span class="pre">mod</span> <span class="pre">p</span></code></p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>a</strong> : integer</p>
<p><strong>p</strong> : positive integer</p>
<p><strong>all_roots</strong> : if True the list of roots is returned or None</p>
</dd>
</dl>
<p class="rubric">Notes</p>
<p>If there is no root it is returned None; else the returned root
is less or equal to <code class="docutils literal notranslate"><span class="pre">p</span> <span class="pre">//</span> <span class="pre">2</span></code>; in general is not the smallest one.
It is returned <code class="docutils literal notranslate"><span class="pre">p</span> <span class="pre">//</span> <span class="pre">2</span></code> only if it is the only root.</p>
<p>Use <code class="docutils literal notranslate"><span class="pre">all_roots</span></code> only when it is expected that all the roots fit
in memory; otherwise use <code class="docutils literal notranslate"><span class="pre">sqrt_mod_iter</span></code>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">sqrt_mod</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt_mod</span><span class="p">(</span><span class="mi">11</span><span class="p">,</span> <span class="mi">43</span><span class="p">)</span>
<span class="go">21</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sqrt_mod</span><span class="p">(</span><span class="mi">17</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
<span class="go">[7, 9, 23, 25]</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.residue_ntheory.sqrt_mod_iter">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.residue_ntheory.</span></span><span class="sig-name descname"><span class="pre">sqrt_mod_iter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">a</span></em>, <em class="sig-param"><span class="pre">p</span></em>, <em class="sig-param"><span class="pre">domain=&lt;class</span> <span class="pre">'int'&gt;</span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/residue_ntheory.py#L301-L357"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.residue_ntheory.sqrt_mod_iter" title="Permalink to this definition">¶</a></dt>
<dd><p>Iterate over solutions to <code class="docutils literal notranslate"><span class="pre">x**2</span> <span class="pre">=</span> <span class="pre">a</span> <span class="pre">mod</span> <span class="pre">p</span></code></p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>a</strong> : integer</p>
<p><strong>p</strong> : positive integer</p>
<p><strong>domain</strong> : integer domain, <code class="docutils literal notranslate"><span class="pre">int</span></code>, <code class="docutils literal notranslate"><span class="pre">ZZ</span></code> or <code class="docutils literal notranslate"><span class="pre">Integer</span></code></p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.residue_ntheory</span> <span class="kn">import</span> <span class="n">sqrt_mod_iter</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">sqrt_mod_iter</span><span class="p">(</span><span class="mi">11</span><span class="p">,</span> <span class="mi">43</span><span class="p">))</span>
<span class="go">[21, 22]</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.residue_ntheory.quadratic_residues">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.residue_ntheory.</span></span><span class="sig-name descname"><span class="pre">quadratic_residues</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">p</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/residue_ntheory.py#L877-L892"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.residue_ntheory.quadratic_residues" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the list of quadratic residues.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.residue_ntheory</span> <span class="kn">import</span> <span class="n">quadratic_residues</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">quadratic_residues</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span>
<span class="go">[0, 1, 2, 4]</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.residue_ntheory.nthroot_mod">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.residue_ntheory.</span></span><span class="sig-name descname"><span class="pre">nthroot_mod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">p</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">all_roots</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/residue_ntheory.py#L810-L874"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.residue_ntheory.nthroot_mod" title="Permalink to this definition">¶</a></dt>
<dd><p>Find the solutions to <code class="docutils literal notranslate"><span class="pre">x**n</span> <span class="pre">=</span> <span class="pre">a</span> <span class="pre">mod</span> <span class="pre">p</span></code></p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>a</strong> : integer</p>
<p><strong>n</strong> : positive integer</p>
<p><strong>p</strong> : positive integer</p>
<p><strong>all_roots</strong> : if False returns the smallest root, else the list of roots</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.residue_ntheory</span> <span class="kn">import</span> <span class="n">nthroot_mod</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nthroot_mod</span><span class="p">(</span><span class="mi">11</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">19</span><span class="p">)</span>
<span class="go">8</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nthroot_mod</span><span class="p">(</span><span class="mi">11</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">19</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
<span class="go">[8, 11]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nthroot_mod</span><span class="p">(</span><span class="mi">68</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">109</span><span class="p">)</span>
<span class="go">23</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.residue_ntheory.is_nthpow_residue">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.residue_ntheory.</span></span><span class="sig-name descname"><span class="pre">is_nthpow_residue</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">m</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/residue_ntheory.py#L618-L644"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.residue_ntheory.is_nthpow_residue" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <code class="docutils literal notranslate"><span class="pre">x**n</span> <span class="pre">==</span> <span class="pre">a</span> <span class="pre">(mod</span> <span class="pre">m)</span></code> has solutions.</p>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r587"><span class="brackets"><a class="fn-backref" href="#id39">R587</a></span></dt>
<dd><ol class="upperalpha simple" start="16">
<li><p>Hackman “Elementary Number Theory” (2009), page 76</p></li>
</ol>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.residue_ntheory.is_quad_residue">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.residue_ntheory.</span></span><span class="sig-name descname"><span class="pre">is_quad_residue</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">p</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/residue_ntheory.py#L582-L615"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.residue_ntheory.is_quad_residue" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns True if <code class="docutils literal notranslate"><span class="pre">a</span></code> (mod <code class="docutils literal notranslate"><span class="pre">p</span></code>) is in the set of squares mod <code class="docutils literal notranslate"><span class="pre">p</span></code>,
i.e a % p in set([i**2 % p for i in range(p)]). If <code class="docutils literal notranslate"><span class="pre">p</span></code> is an odd
prime, an iterative method is used to make the determination:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">is_quad_residue</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="nb">set</span><span class="p">([</span><span class="n">i</span><span class="o">**</span><span class="mi">2</span> <span class="o">%</span> <span class="mi">7</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">7</span><span class="p">)]))</span>
<span class="go">[0, 1, 2, 4]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">j</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">7</span><span class="p">)</span> <span class="k">if</span> <span class="n">is_quad_residue</span><span class="p">(</span><span class="n">j</span><span class="p">,</span> <span class="mi">7</span><span class="p">)]</span>
<span class="go">[0, 1, 2, 4]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.residue_ntheory.legendre_symbol" title="sympy.ntheory.residue_ntheory.legendre_symbol"><code class="xref py py-obj docutils literal notranslate"><span class="pre">legendre_symbol</span></code></a>, <a class="reference internal" href="#sympy.ntheory.residue_ntheory.jacobi_symbol" title="sympy.ntheory.residue_ntheory.jacobi_symbol"><code class="xref py py-obj docutils literal notranslate"><span class="pre">jacobi_symbol</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.residue_ntheory.legendre_symbol">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.residue_ntheory.</span></span><span class="sig-name descname"><span class="pre">legendre_symbol</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">p</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/residue_ntheory.py#L895-L938"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.residue_ntheory.legendre_symbol" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the Legendre symbol <span class="math notranslate nohighlight">\((a / p)\)</span>.</p>
<p>For an integer <code class="docutils literal notranslate"><span class="pre">a</span></code> and an odd prime <code class="docutils literal notranslate"><span class="pre">p</span></code>, the Legendre symbol is
defined as</p>
<div class="math notranslate nohighlight">
\[\begin{split}\genfrac(){}{}{a}{p} = \begin{cases}
     0 &amp; \text{if } p \text{ divides } a\\
     1 &amp; \text{if } a \text{ is a quadratic residue modulo } p\\
    -1 &amp; \text{if } a \text{ is a quadratic nonresidue modulo } p
\end{cases}\end{split}\]</div>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>a</strong> : integer</p>
<p><strong>p</strong> : odd prime</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">legendre_symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">legendre_symbol</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">7</span><span class="p">)]</span>
<span class="go">[0, 1, 1, -1, 1, -1, -1]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">sorted</span><span class="p">(</span><span class="nb">set</span><span class="p">([</span><span class="n">i</span><span class="o">**</span><span class="mi">2</span> <span class="o">%</span> <span class="mi">7</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">7</span><span class="p">)]))</span>
<span class="go">[0, 1, 2, 4]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.residue_ntheory.is_quad_residue" title="sympy.ntheory.residue_ntheory.is_quad_residue"><code class="xref py py-obj docutils literal notranslate"><span class="pre">is_quad_residue</span></code></a>, <a class="reference internal" href="#sympy.ntheory.residue_ntheory.jacobi_symbol" title="sympy.ntheory.residue_ntheory.jacobi_symbol"><code class="xref py py-obj docutils literal notranslate"><span class="pre">jacobi_symbol</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.residue_ntheory.jacobi_symbol">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.residue_ntheory.</span></span><span class="sig-name descname"><span class="pre">jacobi_symbol</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">m</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/residue_ntheory.py#L941-L1026"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.residue_ntheory.jacobi_symbol" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the Jacobi symbol <span class="math notranslate nohighlight">\((m / n)\)</span>.</p>
<p>For any integer <code class="docutils literal notranslate"><span class="pre">m</span></code> and any positive odd integer <code class="docutils literal notranslate"><span class="pre">n</span></code> the Jacobi symbol
is defined as the product of the Legendre symbols corresponding to the
prime factors of <code class="docutils literal notranslate"><span class="pre">n</span></code>:</p>
<div class="math notranslate nohighlight">
\[\genfrac(){}{}{m}{n} =
    \genfrac(){}{}{m}{p^{1}}^{\alpha_1}
    \genfrac(){}{}{m}{p^{2}}^{\alpha_2}
    ...
    \genfrac(){}{}{m}{p^{k}}^{\alpha_k}
    \text{ where } n =
        p_1^{\alpha_1}
        p_2^{\alpha_2}
        ...
        p_k^{\alpha_k}\]</div>
<p>Like the Legendre symbol, if the Jacobi symbol <span class="math notranslate nohighlight">\(\genfrac(){}{}{m}{n} = -1\)</span>
then <code class="docutils literal notranslate"><span class="pre">m</span></code> is a quadratic nonresidue modulo <code class="docutils literal notranslate"><span class="pre">n</span></code>.</p>
<p>But, unlike the Legendre symbol, if the Jacobi symbol
<span class="math notranslate nohighlight">\(\genfrac(){}{}{m}{n} = 1\)</span> then <code class="docutils literal notranslate"><span class="pre">m</span></code> may or may not be a quadratic residue
modulo <code class="docutils literal notranslate"><span class="pre">n</span></code>.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>m</strong> : integer</p>
<p><strong>n</strong> : odd positive integer</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">jacobi_symbol</span><span class="p">,</span> <span class="n">legendre_symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">S</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">jacobi_symbol</span><span class="p">(</span><span class="mi">45</span><span class="p">,</span> <span class="mi">77</span><span class="p">)</span>
<span class="go">-1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">jacobi_symbol</span><span class="p">(</span><span class="mi">60</span><span class="p">,</span> <span class="mi">121</span><span class="p">)</span>
<span class="go">1</span>
</pre></div>
</div>
<p>The relationship between the <code class="docutils literal notranslate"><span class="pre">jacobi_symbol</span></code> and <code class="docutils literal notranslate"><span class="pre">legendre_symbol</span></code> can
be demonstrated as follows:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">L</span> <span class="o">=</span> <span class="n">legendre_symbol</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">S</span><span class="p">(</span><span class="mi">45</span><span class="p">)</span><span class="o">.</span><span class="n">factors</span><span class="p">()</span>
<span class="go">{3: 2, 5: 1}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">jacobi_symbol</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">45</span><span class="p">)</span> <span class="o">==</span> <span class="n">L</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span> <span class="o">*</span> <span class="n">L</span><span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span><span class="o">**</span><span class="mi">1</span>
<span class="go">True</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.residue_ntheory.is_quad_residue" title="sympy.ntheory.residue_ntheory.is_quad_residue"><code class="xref py py-obj docutils literal notranslate"><span class="pre">is_quad_residue</span></code></a>, <a class="reference internal" href="#sympy.ntheory.residue_ntheory.legendre_symbol" title="sympy.ntheory.residue_ntheory.legendre_symbol"><code class="xref py py-obj docutils literal notranslate"><span class="pre">legendre_symbol</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.residue_ntheory.discrete_log">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.residue_ntheory.</span></span><span class="sig-name descname"><span class="pre">discrete_log</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">a</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">order</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prime_order</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/residue_ntheory.py#L1324-L1369"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.residue_ntheory.discrete_log" title="Permalink to this definition">¶</a></dt>
<dd><p>Compute the discrete logarithm of <code class="docutils literal notranslate"><span class="pre">a</span></code> to the base <code class="docutils literal notranslate"><span class="pre">b</span></code> modulo <code class="docutils literal notranslate"><span class="pre">n</span></code>.</p>
<p>This is a recursive function to reduce the discrete logarithm problem in
cyclic groups of composite order to the problem in cyclic groups of prime
order.</p>
<p>It employs different algorithms depending on the problem (subgroup order
size, prime order or not):</p>
<blockquote>
<div><ul class="simple">
<li><p>Trial multiplication</p></li>
<li><p>Baby-step giant-step</p></li>
<li><p>Pollard’s Rho</p></li>
<li><p>Pohlig-Hellman</p></li>
</ul>
</div></blockquote>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">discrete_log</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">discrete_log</span><span class="p">(</span><span class="mi">41</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span>
<span class="go">3</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r588"><span class="brackets"><a class="fn-backref" href="#id40">R588</a></span></dt>
<dd><p><a class="reference external" href="http://mathworld.wolfram.com/DiscreteLogarithm.html">http://mathworld.wolfram.com/DiscreteLogarithm.html</a></p>
</dd>
<dt class="label" id="r589"><span class="brackets"><a class="fn-backref" href="#id41">R589</a></span></dt>
<dd><p>“Handbook of applied cryptography”, Menezes, A. J., Van, O. P. C., &amp;
Vanstone, S. A. (1997).</p>
</dd>
</dl>
</dd></dl>

<span class="target" id="module-sympy.ntheory.continued_fraction"></span><dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.continued_fraction.continued_fraction">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.continued_fraction.</span></span><span class="sig-name descname"><span class="pre">continued_fraction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">a</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/continued_fraction.py#L6-L68"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.continued_fraction.continued_fraction" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the continued fraction representation of a Rational or
quadratic irrational.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.continued_fraction</span> <span class="kn">import</span> <span class="n">continued_fraction</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">continued_fraction</span><span class="p">((</span><span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span><span class="o">*</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span><span class="o">/</span><span class="mi">5</span><span class="p">)</span>
<span class="go">[0, 1, [8, 3, 34, 3]]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.continued_fraction.continued_fraction_periodic" title="sympy.ntheory.continued_fraction.continued_fraction_periodic"><code class="xref py py-obj docutils literal notranslate"><span class="pre">continued_fraction_periodic</span></code></a>, <a class="reference internal" href="#sympy.ntheory.continued_fraction.continued_fraction_reduce" title="sympy.ntheory.continued_fraction.continued_fraction_reduce"><code class="xref py py-obj docutils literal notranslate"><span class="pre">continued_fraction_reduce</span></code></a>, <a class="reference internal" href="#sympy.ntheory.continued_fraction.continued_fraction_convergents" title="sympy.ntheory.continued_fraction.continued_fraction_convergents"><code class="xref py py-obj docutils literal notranslate"><span class="pre">continued_fraction_convergents</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.continued_fraction.continued_fraction_convergents">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.continued_fraction.</span></span><span class="sig-name descname"><span class="pre">continued_fraction_convergents</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cf</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/continued_fraction.py#L303-L351"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.continued_fraction.continued_fraction_convergents" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an iterator over the convergents of a continued fraction (cf).</p>
<p>The parameter should be an iterable returning successive
partial quotients of the continued fraction, such as might be
returned by continued_fraction_iterator.  In computing the
convergents, the continued fraction need not be strictly in
canonical form (all integers, all but the first positive).
Rational and negative elements may be present in the expansion.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.core</span> <span class="kn">import</span> <span class="n">pi</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">S</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.continued_fraction</span> <span class="kn">import</span>             <span class="n">continued_fraction_convergents</span><span class="p">,</span> <span class="n">continued_fraction_iterator</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">continued_fraction_convergents</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]))</span>
<span class="go">[0, 1/2, 1/3, 3/8]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">continued_fraction_convergents</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="n">S</span><span class="p">(</span><span class="s1">&#39;1/2&#39;</span><span class="p">),</span> <span class="o">-</span><span class="mi">7</span><span class="p">,</span> <span class="n">S</span><span class="p">(</span><span class="s1">&#39;1/4&#39;</span><span class="p">)]))</span>
<span class="go">[1, 3, 19/5, 7]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">it</span> <span class="o">=</span> <span class="n">continued_fraction_convergents</span><span class="p">(</span><span class="n">continued_fraction_iterator</span><span class="p">(</span><span class="n">pi</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">7</span><span class="p">):</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">))</span>
<span class="go">3</span>
<span class="go">22/7</span>
<span class="go">333/106</span>
<span class="go">355/113</span>
<span class="go">103993/33102</span>
<span class="go">104348/33215</span>
<span class="go">208341/66317</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.continued_fraction.continued_fraction_iterator" title="sympy.ntheory.continued_fraction.continued_fraction_iterator"><code class="xref py py-obj docutils literal notranslate"><span class="pre">continued_fraction_iterator</span></code></a></p>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.continued_fraction.continued_fraction_iterator">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.continued_fraction.</span></span><span class="sig-name descname"><span class="pre">continued_fraction_iterator</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">x</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/continued_fraction.py#L258-L300"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.continued_fraction.continued_fraction_iterator" title="Permalink to this definition">¶</a></dt>
<dd><p>Return continued fraction expansion of x as iterator.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.core</span> <span class="kn">import</span> <span class="n">Rational</span><span class="p">,</span> <span class="n">pi</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.continued_fraction</span> <span class="kn">import</span> <span class="n">continued_fraction_iterator</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">continued_fraction_iterator</span><span class="p">(</span><span class="n">Rational</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">8</span><span class="p">)))</span>
<span class="go">[0, 2, 1, 2]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">continued_fraction_iterator</span><span class="p">(</span><span class="n">Rational</span><span class="p">(</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="mi">8</span><span class="p">)))</span>
<span class="go">[-1, 1, 1, 1, 2]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">continued_fraction_iterator</span><span class="p">(</span><span class="n">pi</span><span class="p">)):</span>
<span class="gp">... </span>    <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;</span> <span class="mi">7</span><span class="p">:</span>
<span class="gp">... </span>        <span class="k">break</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
<span class="go">3</span>
<span class="go">7</span>
<span class="go">15</span>
<span class="go">1</span>
<span class="go">292</span>
<span class="go">1</span>
<span class="go">1</span>
<span class="go">1</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r590"><span class="brackets"><a class="fn-backref" href="#id42">R590</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Continued_fraction">https://en.wikipedia.org/wiki/Continued_fraction</a></p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.continued_fraction.continued_fraction_periodic">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.continued_fraction.</span></span><span class="sig-name descname"><span class="pre">continued_fraction_periodic</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">p</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">q</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">d</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">0</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">s</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/continued_fraction.py#L71-L178"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.continued_fraction.continued_fraction_periodic" title="Permalink to this definition">¶</a></dt>
<dd><p>Find the periodic continued fraction expansion of a quadratic irrational.</p>
<p>Compute the continued fraction expansion of a rational or a
quadratic irrational number, i.e. <span class="math notranslate nohighlight">\(\frac{p + s\sqrt{d}}{q}\)</span>, where
<span class="math notranslate nohighlight">\(p\)</span>, <span class="math notranslate nohighlight">\(q \ne 0\)</span> and <span class="math notranslate nohighlight">\(d \ge 0\)</span> are integers.</p>
<p>Returns the continued fraction representation (canonical form) as
a list of integers, optionally ending (for quadratic irrationals)
with list of integers representing the repeating digits.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>p</strong> : int</p>
<blockquote>
<div><p>the rational part of the number’s numerator</p>
</div></blockquote>
<p><strong>q</strong> : int</p>
<blockquote>
<div><p>the denominator of the number</p>
</div></blockquote>
<p><strong>d</strong> : int, optional</p>
<blockquote>
<div><p>the irrational part (discriminator) of the number’s numerator</p>
</div></blockquote>
<p><strong>s</strong> : int, optional</p>
<blockquote>
<div><p>the coefficient of the irrational part</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.continued_fraction</span> <span class="kn">import</span> <span class="n">continued_fraction_periodic</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">continued_fraction_periodic</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">7</span><span class="p">)</span>
<span class="go">[2, [1, 4, 1, 1]]</span>
</pre></div>
</div>
<p>Golden ratio has the simplest continued fraction expansion:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">continued_fraction_periodic</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="go">[[1]]</span>
</pre></div>
</div>
<p>If the discriminator is zero or a perfect square then the number will be a
rational number:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">continued_fraction_periodic</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="go">[1, 3]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">continued_fraction_periodic</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">49</span><span class="p">)</span>
<span class="go">[3, 1, 2]</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.continued_fraction.continued_fraction_iterator" title="sympy.ntheory.continued_fraction.continued_fraction_iterator"><code class="xref py py-obj docutils literal notranslate"><span class="pre">continued_fraction_iterator</span></code></a>, <a class="reference internal" href="#sympy.ntheory.continued_fraction.continued_fraction_reduce" title="sympy.ntheory.continued_fraction.continued_fraction_reduce"><code class="xref py py-obj docutils literal notranslate"><span class="pre">continued_fraction_reduce</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r591"><span class="brackets"><a class="fn-backref" href="#id43">R591</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Periodic_continued_fraction">https://en.wikipedia.org/wiki/Periodic_continued_fraction</a></p>
</dd>
<dt class="label" id="r592"><span class="brackets"><a class="fn-backref" href="#id44">R592</a></span></dt>
<dd><p>K. Rosen. Elementary Number theory and its applications.
Addison-Wesley, 3 Sub edition, pages 379-381, January 1992.</p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.continued_fraction.continued_fraction_reduce">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.continued_fraction.</span></span><span class="sig-name descname"><span class="pre">continued_fraction_reduce</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cf</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/continued_fraction.py#L181-L255"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.continued_fraction.continued_fraction_reduce" title="Permalink to this definition">¶</a></dt>
<dd><p>Reduce a continued fraction to a rational or quadratic irrational.</p>
<p>Compute the rational or quadratic irrational number from its
terminating or periodic continued fraction expansion.  The
continued fraction expansion (cf) should be supplied as a
terminating iterator supplying the terms of the expansion.  For
terminating continued fractions, this is equivalent to
<code class="docutils literal notranslate"><span class="pre">list(continued_fraction_convergents(cf))[-1]</span></code>, only a little more
efficient.  If the expansion has a repeating part, a list of the
repeating terms should be returned as the last element from the
iterator.  This is the format returned by
continued_fraction_periodic.</p>
<p>For quadratic irrationals, returns the largest solution found,
which is generally the one sought, if the fraction is in canonical
form (all terms positive except possibly the first).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.continued_fraction</span> <span class="kn">import</span> <span class="n">continued_fraction_reduce</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">continued_fraction_reduce</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">])</span>
<span class="go">225/157</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">continued_fraction_reduce</span><span class="p">([</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])</span>
<span class="go">-256/233</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">continued_fraction_reduce</span><span class="p">([</span><span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">8</span><span class="p">])</span><span class="o">.</span><span class="n">n</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="go">2.718281835</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">continued_fraction_reduce</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">]])</span>
<span class="go">(sqrt(21) + 287)/238</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">continued_fraction_reduce</span><span class="p">([[</span><span class="mi">1</span><span class="p">]])</span>
<span class="go">(1 + sqrt(5))/2</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.continued_fraction</span> <span class="kn">import</span> <span class="n">continued_fraction_periodic</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">continued_fraction_reduce</span><span class="p">(</span><span class="n">continued_fraction_periodic</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">13</span><span class="p">))</span>
<span class="go">(sqrt(13) + 8)/5</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="#sympy.ntheory.continued_fraction.continued_fraction_periodic" title="sympy.ntheory.continued_fraction.continued_fraction_periodic"><code class="xref py py-obj docutils literal notranslate"><span class="pre">continued_fraction_periodic</span></code></a></p>
</div>
</dd></dl>

<span class="target" id="module-sympy.ntheory.digits"></span><dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.digits.count_digits">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.digits.</span></span><span class="sig-name descname"><span class="pre">count_digits</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">10</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/digits.py#L72-L109"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.digits.count_digits" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a dictionary whose keys are the digits of <code class="docutils literal notranslate"><span class="pre">n</span></code> in the
given base, <code class="docutils literal notranslate"><span class="pre">b</span></code>, with keys indicating the digits appearing in the
number and values indicating how many times that digit appeared.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">count_digits</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">count_digits</span><span class="p">(</span><span class="mi">1111339</span><span class="p">)</span>
<span class="go">{1: 4, 3: 2, 9: 1}</span>
</pre></div>
</div>
<p>The digits returned are always represented in base-10
but the number itself can be entered in any format that is
understood by Python; the base of the number can also be
given if it is different than 10:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">n</span> <span class="o">=</span> <span class="mh">0xFA</span><span class="p">;</span> <span class="n">n</span>
<span class="go">250</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">count_digits</span><span class="p">(</span><span class="n">_</span><span class="p">)</span>
<span class="go">{0: 1, 2: 1, 5: 1}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">count_digits</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="mi">16</span><span class="p">)</span>
<span class="go">{10: 1, 15: 1}</span>
</pre></div>
</div>
<p>The default dictionary will return a 0 for any digit that did
not appear in the number. For example, which digits appear 7
times in <code class="docutils literal notranslate"><span class="pre">77!</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">factorial</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c77</span> <span class="o">=</span> <span class="n">count_digits</span><span class="p">(</span><span class="n">factorial</span><span class="p">(</span><span class="mi">77</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="k">if</span> <span class="n">c77</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="mi">7</span><span class="p">]</span>
<span class="go">[1, 3, 7, 9]</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.digits.digits">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.digits.</span></span><span class="sig-name descname"><span class="pre">digits</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">10</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">digits</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/digits.py#L7-L69"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.digits.digits" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list of the digits of <code class="docutils literal notranslate"><span class="pre">n</span></code> in base <code class="docutils literal notranslate"><span class="pre">b</span></code>. The first
element in the list is <code class="docutils literal notranslate"><span class="pre">b</span></code> (or <code class="docutils literal notranslate"><span class="pre">-b</span></code> if <code class="docutils literal notranslate"><span class="pre">n</span></code> is negative).</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>n: integer</strong></p>
<blockquote>
<div><p>The number whose digits are returned.</p>
</div></blockquote>
<p><strong>b: integer</strong></p>
<blockquote>
<div><p>The base in which digits are computed.</p>
</div></blockquote>
<p><strong>digits: integer (or None for all digits)</strong></p>
<blockquote>
<div><p>The number of digits to be returned (padded with zeros, if
necessary).</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.digits</span> <span class="kn">import</span> <span class="n">digits</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">digits</span><span class="p">(</span><span class="mi">35</span><span class="p">)</span>
<span class="go">[10, 3, 5]</span>
</pre></div>
</div>
<p>If the number is negative, the negative sign will be placed on the
base (which is the first element in the returned list):</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">digits</span><span class="p">(</span><span class="o">-</span><span class="mi">35</span><span class="p">)</span>
<span class="go">[-10, 3, 5]</span>
</pre></div>
</div>
<p>Bases other than 10 (and greater than 1) can be selected with <code class="docutils literal notranslate"><span class="pre">b</span></code>:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">digits</span><span class="p">(</span><span class="mi">27</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
<span class="go">[2, 1, 1, 0, 1, 1]</span>
</pre></div>
</div>
<p>Use the <code class="docutils literal notranslate"><span class="pre">digits</span></code> keyword if a certain number of digits is desired:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">digits</span><span class="p">(</span><span class="mi">35</span><span class="p">,</span> <span class="n">digits</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span>
<span class="go">[10, 0, 0, 3, 5]</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.digits.is_palindromic">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.digits.</span></span><span class="sig-name descname"><span class="pre">is_palindromic</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">b</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">10</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/digits.py#L112-L143"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.digits.is_palindromic" title="Permalink to this definition">¶</a></dt>
<dd><p>return True if <code class="docutils literal notranslate"><span class="pre">n</span></code> is the same when read from left to right
or right to left in the given base, <code class="docutils literal notranslate"><span class="pre">b</span></code>.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">is_palindromic</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">all</span><span class="p">(</span><span class="n">is_palindromic</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="p">(</span><span class="o">-</span><span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">22</span><span class="p">,</span> <span class="mi">121</span><span class="p">))</span>
<span class="go">True</span>
</pre></div>
</div>
<p>The second argument allows you to test numbers in other
bases. For example, 88 is palindromic in base-10 but not
in base-8:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">is_palindromic</span><span class="p">(</span><span class="mi">88</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
<p>On the other hand, a number can be palindromic in base-8 but
not in base-10:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="mo">0o121</span><span class="p">,</span> <span class="n">is_palindromic</span><span class="p">(</span><span class="mo">0o121</span><span class="p">)</span>
<span class="go">(81, False)</span>
</pre></div>
</div>
<p>Or it might be palindromic in both bases:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">oct</span><span class="p">(</span><span class="mi">121</span><span class="p">),</span> <span class="n">is_palindromic</span><span class="p">(</span><span class="mi">121</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span> <span class="ow">and</span> <span class="n">is_palindromic</span><span class="p">(</span><span class="mi">121</span><span class="p">)</span>
<span class="go">(&#39;0o171&#39;, True)</span>
</pre></div>
</div>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.ntheory.mobius">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.ntheory.</span></span><span class="sig-name descname"><span class="pre">mobius</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/residue_ntheory.py#L1029-L1084"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.mobius" title="Permalink to this definition">¶</a></dt>
<dd><p>Mobius function maps natural number to {-1, 0, 1}</p>
<dl class="simple">
<dt>It is defined as follows:</dt><dd><ol class="arabic simple">
<li><p><span class="math notranslate nohighlight">\(1\)</span> if <span class="math notranslate nohighlight">\(n = 1\)</span>.</p></li>
<li><p><span class="math notranslate nohighlight">\(0\)</span> if <span class="math notranslate nohighlight">\(n\)</span> has a squared prime factor.</p></li>
<li><p><span class="math notranslate nohighlight">\((-1)^k\)</span> if <span class="math notranslate nohighlight">\(n\)</span> is a square-free positive integer with <span class="math notranslate nohighlight">\(k\)</span>
number of prime factors.</p></li>
</ol>
</dd>
</dl>
<p>It is an important multiplicative function in number theory
and combinatorics.  It has applications in mathematical series,
algebraic number theory and also physics (Fermion operator has very
concrete realization with Mobius Function model).</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>n</strong> : positive integer</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">mobius</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mobius</span><span class="p">(</span><span class="mi">13</span><span class="o">*</span><span class="mi">7</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mobius</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mobius</span><span class="p">(</span><span class="mi">13</span><span class="o">*</span><span class="mi">7</span><span class="o">*</span><span class="mi">5</span><span class="p">)</span>
<span class="go">-1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">mobius</span><span class="p">(</span><span class="mi">13</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span>
<span class="go">0</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r593"><span class="brackets"><a class="fn-backref" href="#id45">R593</a></span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Möbius_function">https://en.wikipedia.org/wiki/M%C3%B6bius_function</a></p>
</dd>
<dt class="label" id="r594"><span class="brackets"><a class="fn-backref" href="#id46">R594</a></span></dt>
<dd><p>Thomas Koshy “Elementary Number Theory with Applications”</p>
</dd>
</dl>
</dd></dl>

<span class="target" id="module-sympy.ntheory.egyptian_fraction"></span><dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.egyptian_fraction.egyptian_fraction">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.egyptian_fraction.</span></span><span class="sig-name descname"><span class="pre">egyptian_fraction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">r</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">algorithm</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'Greedy'</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/egyptian_fraction.py#L7-L137"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.egyptian_fraction.egyptian_fraction" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the list of denominators of an Egyptian fraction
expansion <a class="reference internal" href="#r595" id="id47"><span>[R595]</span></a> of the said rational <span class="math notranslate nohighlight">\(r\)</span>.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>r</strong> : Rational or (p, q)</p>
<blockquote>
<div><p>a positive rational number, <code class="docutils literal notranslate"><span class="pre">p/q</span></code>.</p>
</div></blockquote>
<p><strong>algorithm</strong> : { “Greedy”, “Graham Jewett”, “Takenouchi”, “Golomb” }, optional</p>
<blockquote>
<div><p>Denotes the algorithm to be used (the default is “Greedy”).</p>
</div></blockquote>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">Rational</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.egyptian_fraction</span> <span class="kn">import</span> <span class="n">egyptian_fraction</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">egyptian_fraction</span><span class="p">(</span><span class="n">Rational</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">7</span><span class="p">))</span>
<span class="go">[3, 11, 231]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">egyptian_fraction</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">7</span><span class="p">),</span> <span class="s2">&quot;Graham Jewett&quot;</span><span class="p">)</span>
<span class="go">[7, 8, 9, 56, 57, 72, 3192]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">egyptian_fraction</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">7</span><span class="p">),</span> <span class="s2">&quot;Takenouchi&quot;</span><span class="p">)</span>
<span class="go">[4, 7, 28]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">egyptian_fraction</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">7</span><span class="p">),</span> <span class="s2">&quot;Golomb&quot;</span><span class="p">)</span>
<span class="go">[3, 15, 35]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">egyptian_fraction</span><span class="p">((</span><span class="mi">11</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="s2">&quot;Golomb&quot;</span><span class="p">)</span>
<span class="go">[1, 2, 3, 4, 9, 234, 1118, 2580]</span>
</pre></div>
</div>
<p class="rubric">Notes</p>
<p>Currently the following algorithms are supported:</p>
<ol class="arabic">
<li><p>Greedy Algorithm</p>
<p>Also called the Fibonacci-Sylvester algorithm <a class="reference internal" href="#r596" id="id48"><span>[R596]</span></a>.
At each step, extract the largest unit fraction less
than the target and replace the target with the remainder.</p>
<p>It has some distinct properties:</p>
<ol class="loweralpha simple">
<li><p>Given <span class="math notranslate nohighlight">\(p/q\)</span> in lowest terms, generates an expansion of maximum
length <span class="math notranslate nohighlight">\(p\)</span>. Even as the numerators get large, the number of
terms is seldom more than a handful.</p></li>
<li><p>Uses minimal memory.</p></li>
<li><p>The terms can blow up (standard examples of this are 5/121 and
31/311).  The denominator is at most squared at each step
(doubly-exponential growth) and typically exhibits
singly-exponential growth.</p></li>
</ol>
</li>
<li><p>Graham Jewett Algorithm</p>
<p>The algorithm suggested by the result of Graham and Jewett.
Note that this has a tendency to blow up: the length of the
resulting expansion is always <code class="docutils literal notranslate"><span class="pre">2**(x/gcd(x,</span> <span class="pre">y))</span> <span class="pre">-</span> <span class="pre">1</span></code>.  See <a class="reference internal" href="#r597" id="id49"><span>[R597]</span></a>.</p>
</li>
<li><p>Takenouchi Algorithm</p>
<p>The algorithm suggested by Takenouchi (1921).
Differs from the Graham-Jewett algorithm only in the handling
of duplicates.  See <a class="reference internal" href="#r597" id="id50"><span>[R597]</span></a>.</p>
</li>
<li><p>Golomb’s Algorithm</p>
<p>A method given by Golumb (1962), using modular arithmetic and
inverses.  It yields the same results as a method using continued
fractions proposed by Bleicher (1972).  See <a class="reference internal" href="#r598" id="id51"><span>[R598]</span></a>.</p>
</li>
</ol>
<p>If the given rational is greater than or equal to 1, a greedy algorithm
of summing the harmonic sequence 1/1 + 1/2 + 1/3 + … is used, taking
all the unit fractions of this sequence until adding one more would be
greater than the given number.  This list of denominators is prefixed
to the result from the requested algorithm used on the remainder.  For
example, if r is 8/3, using the Greedy algorithm, we get [1, 2, 3, 4,
5, 6, 7, 14, 420], where the beginning of the sequence, [1, 2, 3, 4, 5,
6, 7] is part of the harmonic sequence summing to 363/140, leaving a
remainder of 31/420, which yields [14, 420] by the Greedy algorithm.
The result of egyptian_fraction(Rational(8, 3), “Golomb”) is [1, 2, 3,
4, 5, 6, 7, 14, 574, 2788, 6460, 11590, 33062, 113820], and so on.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference internal" href="core.html#sympy.core.numbers.Rational" title="sympy.core.numbers.Rational"><code class="xref py py-obj docutils literal notranslate"><span class="pre">sympy.core.numbers.Rational</span></code></a></p>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r595"><span class="brackets">R595</span><span class="fn-backref">(<a href="#id47">1</a>,<a href="#id52">2</a>)</span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Egyptian_fraction">https://en.wikipedia.org/wiki/Egyptian_fraction</a></p>
</dd>
<dt class="label" id="r596"><span class="brackets">R596</span><span class="fn-backref">(<a href="#id48">1</a>,<a href="#id53">2</a>)</span></dt>
<dd><p><a class="reference external" href="https://en.wikipedia.org/wiki/Greedy_algorithm_for_Egyptian_fractions">https://en.wikipedia.org/wiki/Greedy_algorithm_for_Egyptian_fractions</a></p>
</dd>
<dt class="label" id="r597"><span class="brackets">R597</span><span class="fn-backref">(<a href="#id49">1</a>,<a href="#id50">2</a>,<a href="#id54">3</a>)</span></dt>
<dd><p><a class="reference external" href="https://www.ics.uci.edu/~eppstein/numth/egypt/conflict.html">https://www.ics.uci.edu/~eppstein/numth/egypt/conflict.html</a></p>
</dd>
<dt class="label" id="r598"><span class="brackets">R598</span><span class="fn-backref">(<a href="#id51">1</a>,<a href="#id55">2</a>)</span></dt>
<dd><p><a class="reference external" href="http://ami.ektf.hu/uploads/papers/finalpdf/AMI_42_from129to134.pdf">http://ami.ektf.hu/uploads/papers/finalpdf/AMI_42_from129to134.pdf</a></p>
</dd>
</dl>
</dd></dl>

<span class="target" id="module-sympy.ntheory.bbp_pi"></span><dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.bbp_pi.pi_hex_digits">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.bbp_pi.</span></span><span class="sig-name descname"><span class="pre">pi_hex_digits</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prec</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">14</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/bbp_pi.py#L111-L151"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.bbp_pi.pi_hex_digits" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a string containing <code class="docutils literal notranslate"><span class="pre">prec</span></code> (default 14) digits
starting at the nth digit of pi in hex. Counting of digits
starts at 0 and the decimal is not counted, so for n = 0 the
returned value starts with 3; n = 1 corresponds to the first
digit past the decimal point (which in hex is 2).</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory.bbp_pi</span> <span class="kn">import</span> <span class="n">pi_hex_digits</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pi_hex_digits</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="go">&#39;3243f6a8885a30&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">pi_hex_digits</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">&#39;324&#39;</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r599"><span class="brackets"><a class="fn-backref" href="#id56">R599</a></span></dt>
<dd><p><a class="reference external" href="http://www.numberworld.org/digits/Pi/">http://www.numberworld.org/digits/Pi/</a></p>
</dd>
</dl>
</dd></dl>

<span class="target" id="module-sympy.ntheory.ecm"></span><section id="ecm-function">
<h3>ECM function<a class="headerlink" href="#ecm-function" title="Permalink to this headline">¶</a></h3>
<p>The <span class="math notranslate nohighlight">\(ecm\)</span> function is a subexponential factoring algorithm capable of factoring
numbers of around ~35 digits comfortably within few seconds. The time complexity
of <span class="math notranslate nohighlight">\(ecm\)</span> is dependent on the smallest proper factor of the number. So even if the
number is really large but its factors are comparatively smaller then <span class="math notranslate nohighlight">\(ecm\)</span>
can easily factor them. For example we take <span class="math notranslate nohighlight">\(N\)</span> with 15 digit factors
<span class="math notranslate nohighlight">\(15154262241479\)</span>, <span class="math notranslate nohighlight">\(15423094826093\)</span>, <span class="math notranslate nohighlight">\(799333555511111\)</span>, <span class="math notranslate nohighlight">\(809709509409109\)</span>,
<span class="math notranslate nohighlight">\(888888877777777\)</span>, <span class="math notranslate nohighlight">\(914148152112161\)</span>. Now N is a 87 digit number. <span class="math notranslate nohighlight">\(ECM\)</span> takes
under around 47s to factorise this.</p>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.ecm.ecm">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.ecm.</span></span><span class="sig-name descname"><span class="pre">ecm</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">n</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">B1</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">10000</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">B2</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">100000</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">max_curve</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">200</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">seed</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1234</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/ecm.py#L274-L320"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.ecm.ecm" title="Permalink to this definition">¶</a></dt>
<dd><p>Performs factorization using Lenstra’s Elliptic curve method.</p>
<p>This function repeatedly calls <span class="math notranslate nohighlight">\(ecm_one_factor\)</span> to compute the factors
of n. First all the small factors are taken out using trial division.
Then <span class="math notranslate nohighlight">\(ecm_one_factor\)</span> is used to compute one factor at a time.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>n</strong> : Number to be Factored</p>
<p><strong>B1</strong> : Stage 1 Bound</p>
<p><strong>B2</strong> : Stage 2 Bound</p>
<p><strong>max_curve</strong> : Maximum number of curves generated</p>
<p><strong>seed</strong> : Initialize pseudorandom generator</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">ecm</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ecm</span><span class="p">(</span><span class="mi">25645121643901801</span><span class="p">)</span>
<span class="go">{5394769, 4753701529}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ecm</span><span class="p">(</span><span class="mi">9804659461513846513</span><span class="p">)</span>
<span class="go">{4641991, 2112166839943}</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="examples">
<h3>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h3>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">ecm</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ecm</span><span class="p">(</span><span class="mi">7060005655815754299976961394452809</span><span class="p">,</span> <span class="n">B1</span><span class="o">=</span><span class="mi">100000</span><span class="p">,</span> <span class="n">B2</span><span class="o">=</span><span class="mi">1000000</span><span class="p">)</span>
<span class="go">{6988699669998001, 1010203040506070809}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ecm</span><span class="p">(</span><span class="mi">122921448543883967430908091422761898618349713604256384403202282756086473494959648313841</span><span class="p">,</span> <span class="n">B1</span><span class="o">=</span><span class="mi">100000</span><span class="p">,</span> <span class="n">B2</span><span class="o">=</span><span class="mi">1000000</span><span class="p">)</span>
<span class="go">{15154262241479,</span>
<span class="go">15423094826093,</span>
<span class="go">799333555511111,</span>
<span class="go">809709509409109,</span>
<span class="go">888888877777777,</span>
<span class="go">914148152112161}</span>
</pre></div>
</div>
<span class="target" id="module-sympy.ntheory.qs"></span></section>
<section id="qs-function">
<h3>QS function<a class="headerlink" href="#qs-function" title="Permalink to this headline">¶</a></h3>
<p>The <span class="math notranslate nohighlight">\(qs\)</span> function is a subexponential factoring algorithm, the fastest
factoring algorithm for numbers within 100 digits. The time complexity of
<span class="math notranslate nohighlight">\(qs\)</span> is dependent on the size of the number so it is used if the number contains
large factors. Due to this while factoring numbers first <span class="math notranslate nohighlight">\(ecm\)</span> is used to get
smaller factors of around ~15 digits then <span class="math notranslate nohighlight">\(qs\)</span> is used to get larger factors.</p>
<p>For factoring <span class="math notranslate nohighlight">\(2709077133180915240135586837960864768806330782747\)</span> which is a semi-prime number
with two 25 digit factors. <span class="math notranslate nohighlight">\(qs\)</span> is able to factorize this in around 248s.</p>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.ntheory.qs.qs">
<span class="sig-prename descclassname"><span class="pre">sympy.ntheory.qs.</span></span><span class="sig-name descname"><span class="pre">qs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">N</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">prime_bound</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">M</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ERROR_TERM</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">25</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">seed</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1234</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/ntheory/qs.py#L440-L515"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.ntheory.qs.qs" title="Permalink to this definition">¶</a></dt>
<dd><p>Performs factorization using Self-Initializing Quadratic Sieve.
In SIQS, let N be a number to be factored, and this N should not be a
perfect power. If we find two integers such that <code class="docutils literal notranslate"><span class="pre">X**2</span> <span class="pre">=</span> <span class="pre">Y**2</span> <span class="pre">modN</span></code> and
<code class="docutils literal notranslate"><span class="pre">X</span> <span class="pre">!=</span> <span class="pre">+-Y</span> <span class="pre">modN</span></code>, then <span class="math notranslate nohighlight">\(gcd(X + Y, N)\)</span> will reveal a proper factor of N.
In order to find these integers X and Y we try to find relations of form
t**2 = u modN where u is a product of small primes. If we have enough of
these relations then we can form <code class="docutils literal notranslate"><span class="pre">(t1*t2...ti)**2</span> <span class="pre">=</span> <span class="pre">u1*u2...ui</span> <span class="pre">modN</span></code> such that
the right hand side is a square, thus we found a relation of <code class="docutils literal notranslate"><span class="pre">X**2</span> <span class="pre">=</span> <span class="pre">Y**2</span> <span class="pre">modN</span></code>.</p>
<p>Here, several optimizations are done like using muliple polynomials for
sieving, fast changing between polynomials and using partial relations.
The use of partial relations can speeds up the factoring by 2 times.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>N</strong> : Number to be Factored</p>
<p><strong>prime_bound</strong> : upper bound for primes in the factor base</p>
<p><strong>M</strong> : Sieve Interval</p>
<p><strong>ERROR_TERM</strong> : Error term for checking smoothness</p>
<p><strong>threshold</strong> : Extra smooth relations for factorization</p>
<p><strong>seed</strong> : generate pseudo prime numbers</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">qs</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">qs</span><span class="p">(</span><span class="mi">25645121643901801</span><span class="p">,</span> <span class="mi">2000</span><span class="p">,</span> <span class="mi">10000</span><span class="p">)</span>
<span class="go">{5394769, 4753701529}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">qs</span><span class="p">(</span><span class="mi">9804659461513846513</span><span class="p">,</span> <span class="mi">2000</span><span class="p">,</span> <span class="mi">10000</span><span class="p">)</span>
<span class="go">{4641991, 2112166839943}</span>
</pre></div>
</div>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r600"><span class="brackets"><a class="fn-backref" href="#id57">R600</a></span></dt>
<dd><p><a class="reference external" href="https://pdfs.semanticscholar.org/5c52/8a975c1405bd35c65993abf5a4edb667c1db.pdf">https://pdfs.semanticscholar.org/5c52/8a975c1405bd35c65993abf5a4edb667c1db.pdf</a></p>
</dd>
<dt class="label" id="r601"><span class="brackets"><a class="fn-backref" href="#id58">R601</a></span></dt>
<dd><p><a class="reference external" href="https://www.rieselprime.de/ziki/Self-initializing_quadratic_sieve">https://www.rieselprime.de/ziki/Self-initializing_quadratic_sieve</a></p>
</dd>
</dl>
</dd></dl>

</section>
<section id="id59">
<h3>Examples<a class="headerlink" href="#id59" title="Permalink to this headline">¶</a></h3>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.ntheory</span> <span class="kn">import</span> <span class="n">qs</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">qs</span><span class="p">(</span><span class="mi">5915587277</span><span class="o">*</span><span class="mi">3267000013</span><span class="p">,</span> <span class="mi">1000</span><span class="p">,</span> <span class="mi">10000</span><span class="p">)</span>
<span class="go">{3267000013, 5915587277}</span>
</pre></div>
</div>
</section>
</section>
</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Number Theory</a><ul>
<li><a class="reference internal" href="#ntheory-class-reference">Ntheory Class Reference</a></li>
<li><a class="reference internal" href="#ntheory-functions-reference">Ntheory Functions Reference</a><ul>
<li><a class="reference internal" href="#ecm-function">ECM function</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
<li><a class="reference internal" href="#qs-function">QS function</a></li>
<li><a class="reference internal" href="#id59">Examples</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="matrices/expressions.html"
                        title="previous chapter">Matrix Expressions</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="numeric-computation.html"
                        title="next chapter">Numeric Computation</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/modules/ntheory.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="numeric-computation.html" title="Numeric Computation"
             >next</a> |</li>
        <li class="right" >
          <a href="matrices/expressions.html" title="Matrix Expressions"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="index.html" >SymPy Modules Reference</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Number Theory</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/ntheory.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:24:32 GMT -->
</html>